The Python Oracle

How to use a conditional statement based on DataFrame boolean value in pandas

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------


Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT


Music by Eric Matyas
https://www.soundimage.org
Track title: The World Wide Mind

--

Chapters
00:00 How To Use A Conditional Statement Based On Dataframe Boolean Value In Pandas
00:57 Accepted Answer Score 19
02:08 Answer 2 Score 0
02:19 Thank you

--

Full question
https://stackoverflow.com/questions/3271...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas

#avk47



ACCEPTED ANSWER

Score 19


If you want to check if any row of the DataFrame meets your conditions you can use .any() along with your condition . Example -

if ((df['column1']=='banana') & (df['colour']=='green')).any():

Example -

In [16]: df
Out[16]:
   A  B
0  1  2
1  3  4
2  5  6

In [17]: ((df['A']==1) & (df['B'] == 2)).any()
Out[17]: True

This is because your condition - ((df['column1']=='banana') & (df['colour']=='green')) - returns a Series of True/False values.

This is because in pandas when you compare a series against a scalar value, it returns the result of comparing each row of that series against the scalar value and the result is a series of True/False values indicating the result of comparison of that row with the scalar value. Example -

In [19]: (df['A']==1)
Out[19]:
0     True
1    False
2    False
Name: A, dtype: bool

In [20]: (df['B'] == 2)
Out[20]:
0     True
1    False
2    False
Name: B, dtype: bool

And the & does row-wise and for the two series. Example -

In [18]: ((df['A']==1) & (df['B'] == 2))
Out[18]:
0     True
1    False
2    False
dtype: bool

Now to check if any of the values from this series is True, you can use .any() , to check if all the values in the series are True, you can use .all() .




ANSWER 2

Score 0


You can also try to use lambda function and write any quick expression.

example:

total_votes_df.apply(lambda x: True if x['VOTES'] < 0.16666666666666668 * x['TOTAL_VOTES'] else False, axis=1)