Skip to main content

Python Pandas: Access Data using Label

In Pandas we can also access elements from series by using index label or name. For using this method we have to set values to index label. As we know series is like a dictionary so we can get the set of values by using index label.

Example

import pandas as pd
data=['P','R','O','G','R','A','M','S','B','U','Z','Z']
series_data=pd.Series(data, index=['A','B','C','D','E','F','G','H','I','J','K','L'])
print(series_data['A'])

Output: P

print(series_data['H'])

Output: S

print(series_data[['A','C','E','G','I','K']])

Output

A    P
C    O
E    R
G    M
I    B
K    Z
dtype: object
print(series_data[['B','D','F','H','J','L']])

Output

B    R
D    G
F    A
H    S
J    U
L    Z
dtype: object
Submitted by devanshi.srivastava on November 3, 2021

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.