Home > Software design >  find one value in pandas data frame
find one value in pandas data frame

Time:09-21

I tried multiple ways. I have below data frame and I have EMP ID from other process. I want to find EMP name based on EMP ID.

EMP ID  Name EMP    Location
1      John A       New York
2      Paul         London
3      Adam K       London
4      Lawrence L   London

below code in not working 

emp_name= df['EMP ID']==id_no
print(emp_name[Name Emp])

I want Name - Paul for EMP ID =2

Am I passing EMP ID right in data frame? I tried df.query also. Am I doing something wrong?

CodePudding user response:

By doing df["EMP ID"]==id_no, you will get a series of boolean which represents a result of elementwise comparison:

df["EMP ID"] == 2

0    False
1     True
2    False
3    False
Name: EMP ID, dtype: bool

Assuming EMP ID is unique for each Name EMP, you can make a new series that can act as a dict:

s = df.set_index("EMP ID")["Name EMP"]
s[id_no]

Output:

"Paul"

CodePudding user response:

df.loc[df['EMP ID']==id_no]['Name EMP']

This will return 'Paul' as the output for id_no=2

  • Related