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

How can I export single value out of pandas ataframe using where function?

my pandas dataframe is like this:

          a        b                         c
0         10       hi                       sth
1         300      hello                    0
2         2157     bye                      any

my query is like:

df[a].where(df['b'] == 'hi')

the result shows all the column a with the value for where b condition is true.
My question is how can I have a single value (as for my case only one row is true) as the result istead of a list, Which in my example is "10"

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

>Solution :

You can use loc and squeeze:

df.loc[df['b'].eq('hi'), 'a'].squeeze()

output: 10

Note that in case you have more than one match, you will get a Series as output.

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