Series Data Structure¶

A Series is a one-dimensional object that can hold any data type such as integers, floats and strings, Python Objects and etc.

pandas.Series( data, index, dtype, copy)
data: data takes various forms like ndarray, list, constants
index: Index values must be unique and hashable, same length as data. Default np.arrange(n) if no index is passed.
dtype: dtype is for data type. If None, data type will be inferred
copy: Copy data. Default False

Creating a Series¶

A pandas Series can be created using 'pandas.Series()' command.

A Series can be created by using the various inputs like:

  1. List
  2. Tuple
  3. Dictionary
  4. Scalar value or constant
  5. Numpy Array

Empty Series¶

In [1]:
import pandas as pd
s=pd.Series()
s
C:\Users\karan\AppData\Local\Temp\ipykernel_5576\2024902043.py:2: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  s=pd.Series()
Out[1]:
Series([], dtype: float64)

1. Create a Series from List¶

In [2]:
L=[69,51,87,97,30,16]
s=pd.Series(L)
s
Out[2]:
0    69
1    51
2    87
3    97
4    30
5    16
dtype: int64

add index to series¶

In [3]:
L=[69,51,87,97,30,16]
s=pd.Series(L,index=['a','b','c','d','e','f'])
s
Out[3]:
a    69
b    51
c    87
d    97
e    30
f    16
dtype: int64

change data dtype of series¶

In [4]:
L=[69,51,87,97,30,16]
s=pd.Series(L,index=['a','b','c','d','e','f'],dtype=float)
s
Out[4]:
a    69.0
b    51.0
c    87.0
d    97.0
e    30.0
f    16.0
dtype: float64

2. Create a Series from Tuple¶

In [5]:
T=(69,51,87,97,30,16)
s=pd.Series(T,index=['a','b','c','d','e','f'],dtype=float)
s
Out[5]:
a    69.0
b    51.0
c    87.0
d    97.0
e    30.0
f    16.0
dtype: float64

3. Create a Series from Dictionary¶

If no index is supplied, the dictionary keys are taken in sorted order to generate the index if a dict is passed as input. If index is specified, the values in data that correspond to the index's labels will be extracted.

In [6]:
data = {'a':50, 'b':18, 'c':69, 'd':64, 'e':13}
s = pd.Series(data)
s
Out[6]:
a    50
b    18
c    69
d    64
e    13
dtype: int64

pass index¶

In [7]:
data = {'a':50, 'b':18, 'c':69, 'd':64, 'e':13}
s = pd.Series(data,index=['b','d','a','c','e'])
s
Out[7]:
b    18
d    64
a    50
c    69
e    13
dtype: int64

add unknow index¶

In [8]:
data = {'a':50, 'b':18, 'c':69, 'd':64, 'e':13}
s = pd.Series(data,index=['b','d','f','a','c','e','g'])
s
Out[8]:
b    18.0
d    64.0
f     NaN
a    50.0
c    69.0
e    13.0
g     NaN
dtype: float64

Note: Here NaN is : Not a Number

  • Index order is persisted and the missing element is filled with NaN (Not a Number)

4. Create a Series with Scalar value or constant¶

  • data can be a scalar, which is repeated to fill the specified index:
In [9]:
import pandas as pd 
Ser = pd.Series(5)
Ser
Out[9]:
0    5
dtype: int64
In [10]:
import pandas as pd 
Ser = pd.Series(5, index=[100, 200, 300,400,500])
Ser
Out[10]:
100    5
200    5
300    5
400    5
500    5
dtype: int64

5. 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].

In [11]:
import pandas as pd
import numpy as np

data = np.array([10,20,30,40,50])
s = pd.Series(data)
s
Out[11]:
0    10
1    20
2    30
3    40
4    50
dtype: int32

Passing the index values¶

In [12]:
import pandas as pd
import numpy as np

data = np.array([10,20,30,40,50])
s = pd.Series(data,index=['a','b','c','d','e'])
s
Out[12]:
a    10
b    20
c    30
d    40
e    50
dtype: int32

6. Accessing Data from Series with Position¶

In [13]:
L=[69,51,87,97,30,16]
s=pd.Series(L,index=['a','b','c','d','e','f'])
s
Out[13]:
a    69
b    51
c    87
d    97
e    30
f    16
dtype: int64

retrieve the first element using default index¶

In [14]:
print(s[0])
69

or¶

In [15]:
print(s['a'])
69

retrieve the first three element¶

In [16]:
print(s[:3])
a    69
b    51
c    87
dtype: int64

or¶

In [17]:
print(s[:'c'])
a    69
b    51
c    87
dtype: int64

retrieve the last three element¶

In [18]:
print(s[-3:])
d    97
e    30
f    16
dtype: int64

or¶

In [19]:
print(s['d':])
d    97
e    30
f    16
dtype: int64

retrieve multiple elements¶

In [20]:
print(s[['a','c','d']])
a    69
c    87
d    97
dtype: int64