32.pandas series

Series is a one-dimensional labeled array capable of holding data of any type (integer, string, float, python objects, etc.). The axis labels are collectively called index.

pandas.series

pandas.Series( data, index, dtype, copy)

Create an empty series

import pandas as pd
s = pd.Series()
print s
>>> Series([], dtype: float64)

Create a Series from ndarray

If data is an ndarray, then index passed must be of the same length. If no index is passed, then by default index will be range(n) where n is array length, i.e., [0,1,2,3…. range(len(array))-1].

Example

import pandas as pd
import numpy as np
data = np.array(['a','b','c','d'])
s = pd.Series(data)
print(s)

>>> 0   a
    1   b
    2   c
    3   d
    dtype: object

We did not pass any index, so by default, it assigned the indexes ranging from 0 to len(data)-1, i.e., 0 to 3.

Example

import pandas as pd
import numpy as np
data = np.array(['a','b','c','d'])
s = pd.Series(data,index=[100,101,102,103])
print(s)
>>> 100  a
    101  b
    102  c
    103  d
    dtype: object

We passed the index values here. Now we can see the customized indexed values in the output.

Create a Series from dict

A dict can be passed as input and if no index is specified, then the dictionary keys are taken in a sorted order to construct index. If index is passed, the values in data corresponding to the labels in the index will be pulled out.

Example

import pandas as pd
import numpy as np
data = {'a' : 0., 'b' : 1., 'c' : 2.}
s = pd.Series(data)
print(s)
>>> a 0.0
    b 1.0
    c 2.0
    dtype: float64
Dictionary keys are used to construct index.

Example

import pandas as pd
import numpy as np
data = {'a' : 0., 'b' : 1., 'c' : 2.}
s = pd.Series(data,index=['b','c','d','a'])
print(s)
>>> b 1.0
    c 2.0
    d NaN
    a 0.0
    dtype: float64
Index order is persisted and the missing element is filled with NaN (Not a Number).

Create a series from scaler

If data is a scalar value, an index must be provided. The value will be repeated to match the length of index.

Example

import pandas as pd
import numpy as np
s = pd.Series(5, index=[0, 1, 2, 3])
print(s)
>>> 0  5
    1  5
    2  5
    3  5
    dtype: int64

Accessing Data from Series with Position

Data in the series can be accessed similar to that in an ndarray.

Example

import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])

#retrieve the first element
print(s[0])
>>> 1
#retrieve the first three element
print(s[:3])
>>> a  1
    b  2
    c  3
    dtype: int64
#retrieve the last three element
print(s[-3:])
>>> c  3
    d  4
    e  5
    dtype: int64

Retrieve Data Using Label (Index)

A Series is like a fixed-size dict in that you can get and set values by index label.

Example

import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])

#retrieve a single element
print(s['a'])
>>> 1
#retrieve multiple elements
print(s[['a','c','d']])
>>> a  1
    c  3
    d  4
    dtype: int64
print(s['f'])
>>> KeyError: 'f'