Pandas series is an one dimensional data like Python List or Array. It is capable to hold any types of data. It has own methods and attributes.

Image credit: Book(Learning the pandas library)
Creating a Series
Pandas Series can be created by the constructer
series=pd.Series(data=None,index=None,dtype=None,name=None,copy=False)
1. data can be of various types like ndarray, list, dictionary, scalar etc.
2. index array like or hashable and have same length as data.
3. dtype is for data type by default it inferred.
4. name to given the Series
5. copy data by default False
Pandas Series can be create by List, Dictionary, Array, Scalar or Python tuple etc but in real world we can create series from real datasets either from csv, excel or other data sources like SQL.
Creating an empty list
import pandas as pd
series = pd.Series()
print(series)
Output
Series([], dtype: float64)
Creating a Series from List
import pandas as pd
Creating a Series from List
word=['H','Y','P','E','R','H','A','C','K']
series=pd.Series(word)
print(series)
Output
0 H
1 Y
2 P
3 E
4 R
5 H
6 A
7 C
8 K
dtype: object
Creating a Series from Dictionary
import pandas as pd
#creating a Series from Dictionary
info={'Name':'Mitra','Branch':'CSE','Org':'Hyperhack Lab'}
series=pd.Series(add)
print(series)
Output:
Name Mitra
Branch CSE
Org Hypehack Lab
dtype: object
Creating a Series from Numpy Array.
import numpy as np
import pandas as pd
data=np.array(['H','Y','P','E','R','H','A','C','K'])
series=pd.Series(data)
print(series)
Output
0 H
1 Y
2 P
3 E
4 R
5 H
6 A
7 C
8 K
dtype: object
Creating a Series from Scalar
import pandas as pd
series = pd.Series(6, index =[0, 1, 2, 3, 4])
print(series)
Ouput
0 6
1 6
2 6
3 6
4 6
dtype: int64
Accessing or Retrieving elements from Series
There are two ways to access or retrieve elements from series
- Access or Retrieve data from series with position
- Access or Retrieve data from series with label
Access or Retrieve data from series with position: In this method we retrieve data by it’s position using [] bracket, since elements position start from 0 so the total length of series element’s will be n-1.
Example 1: Access or retrieve 5 elements from beginning from the Series.
import pandas as pd
data =(['H','Y','P','E','R','H', 'A','C','K','L','A','B'])
series = pd.Series(data)
print(series[:5])
Output
0 H
1 Y
2 P
3 E
4 R
dtype: object
Example 2: Access or retrieve 3rd to 8th elements from the Series.
import pandas as pd
data =(['H','Y','P','E','R','H', 'A','C','K','L','A','B'])
series = pd.Series(data)
print(series[2:8])
Output
2 P
3 E
4 R
5 H
6 A
7 C
dtype: object
Access or Retrieve data from series with Label or Index: A Series is a fixed sized dictionary in that we can get and set value by index. Therefore we have to set values by index label.
Example 1: Access or retrieve 12 index element from Series.
import pandas as pd
data =(['H','Y','P','E','R','H', 'A','C','K','L','A','B'])
series = pd.Series(data,index=[11,12,13,14,15,16,17,18,19,20,21,22])
print(series[12])
Output
Y
Example 2: Access or retrieve 13,14,18,22 index elements from the Series.
import pandas as pd
data =(['H','Y','P','E','R','H', 'A','C','K','L','A','B'])
series = pd.Series(data,index=[11,12,13,14,15,16,17,18,19,20,21,22])
print(series[[13,14,18,22]])
Output
13 P
14 E
18 C
22 B
dtype: object
Leave a Reply
You must be logged in to post a comment.