3

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']]

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot! wish I asked earlier to save that wasted 1 hour :')
Glad to be helpful. Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.