Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting cell value by row name and column name from dataframe

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading