How to access Data from Pandas Series | Retrieving Data from Series with easy example

 How to access Data from Pandas Series

access Data from pandas Series

In this post, we will learn how to access data from pandas Series. To access the elements from Series we use the index operator [ ].

In the previous post, we have learned about how to create pandas series. If you want to learn how to create a Series the link is given below-

How to create Pandas Series 

 We can access data from Series in two ways

 1. Access data from pandas Series with position

Data from the Series can be accessed similar to that in an array.

Accessing the first element

As we know the counting starts from zero for the array which means the first element is stored at the zeroth position and so on.

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

its output is as follows

1

Accessing the last element

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

its output is as follows

5

Accessing the first four element

import pandas as pd
s=pd.Series([7,3,9,4,1,8,2], index=[‘a’,’b’,’c’,’d’,’e’,’f’,’g’])
print (s[:4])

its output is as follows

a7
b3
c9
d4

 2. Accessing data from Series using index (label)

Accessing a single element using index value

import pandas as pd
s=pd.Series([1,2,3,4,5], index=[‘a’,’b’,’c’,’d’,’e’])
print (s[‘a’])
 
its output will be
1
 

Accessing multiple elements using a list of index values

import pandas as pd
s=pd.Series([1,2,3,4,5], index=[‘a’,’b’,’c’,’d’,’e’])
print (s[ [‘a’,’b’,’e’] ])
 
its output will be
a    1
b    2
e    5
 
 If you like my post please share it with your friends by simply clicking the below social media button, and don’t forget to comment so we will update ourselves.
Share in a Single Click

Leave a Reply

Your email address will not be published. Required fields are marked *