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:
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.