-1

I have following format of DataFrame:

# import pandas library
import pandas as pd
  
# Creating a dictionary
d = {'id': ['a', 'b', 'c'],
     'label': ['bal', 'sal', 'tal'], 
     } 
  
# Creating a Dataframe
df = pd.DataFrame(d) 
  
# show the dataframe
print(df) 

Output:

  id label
0  a   bal
1  b   sal
2  c   tal

I have id = 'b'. Now I want to access the corresponding label value like label = 'sal'.

How can I do this?

Md. Rezwanul Haque
  • 2,882
  • 7
  • 28
  • 45

2 Answers2

4

Like this:

In [21]: df.set_index('id').loc['b', 'label']
Out[21]: 'sal'

Or, use df.query:

In [28]: df.query('id == "b"')['label']
Out[28]: 
1    sal
Name: label, dtype: object
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
1

Is this what you need?

df.loc[df.id == 'b', 'label']

Paul
  • 1,801
  • 1
  • 12
  • 18