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

Time:05-17

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"

CodePudding user response:

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.

  • Related