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
Wed, 11/03/2021 - 20:20

Authored by

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