Jaken Herman Jaken Herman - 2 months ago 7
Python Question

Finding value of another attribute given an attribute

I have a CSV that has multiple lines, and I am looking to find the

JobTitle
of a person, given their name. The CSV is now in a DataFrame
sal
as such:

id employee_name job_title
1 SOME NAME SOME TITLE


I'm trying to find the
JobTitle
of some given persons name, but am having a hard time doing this. I am currently trying to learn pandas by doing crash courses and I know I can get a list of job titles by using
sal['job_title']
, but that gives me an entire list of the job titles.

How can I find the value of a specific person?

Answer

You need boolean indexing:

sal[sal.employee_name == 'name']

If need select only some column, use ix with boolean indexing:

sal.ix[sal.employee_name == 'name', 'job_title']

Sample:

sal = pd.DataFrame({'id':[1,2,3],
                   'employee_name':['name','name1','name2'],
                   'job_title':['titleA','titleB','titleC']},
                    columns=['id','employee_name','job_title'])

print (sal)
   id employee_name job_title
0   1          name    titleA
1   2         name1    titleB
2   3         name2    titleC

print (sal[sal.employee_name == 'name'])
   id employee_name job_title
0   1          name    titleA

print (sal.ix[sal.employee_name == 'name', 'job_title'])
0    titleA
Name: job_title, dtype: object
Comments