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:
- List
- Tuple
- Dictionary
- Scalar value or constant
- Numpy Array
Empty Series¶
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()
Series([], dtype: float64)
1. Create a Series from List¶
L=[69,51,87,97,30,16]
s=pd.Series(L)
s
0 69 1 51 2 87 3 97 4 30 5 16 dtype: int64
add index to series¶
L=[69,51,87,97,30,16]
s=pd.Series(L,index=['a','b','c','d','e','f'])
s
a 69 b 51 c 87 d 97 e 30 f 16 dtype: int64
change data dtype of series¶
L=[69,51,87,97,30,16]
s=pd.Series(L,index=['a','b','c','d','e','f'],dtype=float)
s
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¶
T=(69,51,87,97,30,16)
s=pd.Series(T,index=['a','b','c','d','e','f'],dtype=float)
s
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.
data = {'a':50, 'b':18, 'c':69, 'd':64, 'e':13}
s = pd.Series(data)
s
a 50 b 18 c 69 d 64 e 13 dtype: int64
pass index¶
data = {'a':50, 'b':18, 'c':69, 'd':64, 'e':13}
s = pd.Series(data,index=['b','d','a','c','e'])
s
b 18 d 64 a 50 c 69 e 13 dtype: int64
add unknow index¶
data = {'a':50, 'b':18, 'c':69, 'd':64, 'e':13}
s = pd.Series(data,index=['b','d','f','a','c','e','g'])
s
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:
import pandas as pd
Ser = pd.Series(5)
Ser
0 5 dtype: int64
import pandas as pd
Ser = pd.Series(5, index=[100, 200, 300,400,500])
Ser
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].
import pandas as pd
import numpy as np
data = np.array([10,20,30,40,50])
s = pd.Series(data)
s
0 10 1 20 2 30 3 40 4 50 dtype: int32
Passing the index values¶
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
a 10 b 20 c 30 d 40 e 50 dtype: int32
6. Accessing Data from Series with Position¶
L=[69,51,87,97,30,16]
s=pd.Series(L,index=['a','b','c','d','e','f'])
s
a 69 b 51 c 87 d 97 e 30 f 16 dtype: int64
retrieve the first element using default index¶
print(s[0])
69
or¶
print(s['a'])
69
retrieve the first three element¶
print(s[:3])
a 69 b 51 c 87 dtype: int64
or¶
print(s[:'c'])
a 69 b 51 c 87 dtype: int64
retrieve the last three element¶
print(s[-3:])
d 97 e 30 f 16 dtype: int64
or¶
print(s['d':])
d 97 e 30 f 16 dtype: int64
retrieve multiple elements¶
print(s[['a','c','d']])
a 69 c 87 d 97 dtype: int64