Imagine a Data Table like Excel sheet or SQL Table, In that table there are rows, columns and an index.
Series is just like a column which is 1 Dimensional. Additionally a series has an index also.
It is a one-dimensional array holding data of any type.
Properties of a Pandas Series
- Pandas Series is Indexed
- Pandas Series is Mutable
- Pandas Series is Ordered
Pandas Series can be created using a list or any similar object.
Example:
import pandas as pd
ml = [2,8, 3]
s= pd.Series(ml)
print(s)
In the above given example, ml is a list, s is the series that we are creating. This code will produce following output
0 2 1 8 2 3 dtype: int64
In this output the first column is called Index and the second column has our Values
In total it is a Single Dimensional Data.
If you want you can change the Index as per your liking
import pandas as pd
a = [3, 7, 9]
s= pd.Series(a, index = [\”x\”, \”y\”, \”z\”])
print(s)
Output:
x 1 y 7 z 2 dtype: int64
Here you can see the index has been changed.
\”s\” is the main object here which is of Pandas Series type.
How to access values in a Series using Index
In the above given example we can access values by writing following method
print(s[‘x’]) # when the index is non numerical
print(s[2]) # when the index is integer
How to Create Pandas Series from a Python Dictionary:
import pandas as pd
marks= {“Mohan”: 420, “Sohan”: 380, “John”: 390}
s = pd.Series(marks)
print(s)
# Here s is a series
Mohan 420 Sohan 380 John 390 dtype: int64 So now if we have to get marks of Sohan , we can use print(s['Sohan'])
We will see more use of Series in other chapters as well.