Home > Net >  Getting cell value by row name and column name from dataframe
Getting cell value by row name and column name from dataframe

Time:06-21

Let's say I have the following data frame

     name             age     favorite_color  grade
0    Willard Morris   20           blue     88
1       Al Jennings   19           blue     92
2      Omar Mullins   22         yellow     95
3  Spencer McDaniel   21          green     70

And I'm trying to get the grade for Omar which is "95"

it can be easily obtained using

ddf = df.loc[[2], ['grade']]
print(ddf)

However, I want to use his name "Omar" instead of using the raw index "2".

Is it possible?

I tried the following syntax but it didn't work

ddf = df.loc[['Omar Mullins'], ['grade']]

CodePudding user response:

Try this:

 ddf = df[df['name'] == 'Omar Mullins']['grade']

to output the grade values.

Instead:

ddf = df[df['name'] == 'Omar Mullins']

will output the full row.

  • Related