The Python Oracle

How to check if all values in a dataframe are True

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Ocean Floor

--

Chapters
00:00 Question
00:57 Accepted answer (Score 16)
01:49 Answer 2 (Score 3)
02:17 Thank you

--

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

--

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

--

Tags
#python #pandas

#avk47



ACCEPTED ANSWER

Score 18


First of all, I do not advise this. Please do not use mixed dtypes inside your dataframe columns - that defeats the purpose of dataframes and they are no more useful than lists and no more performant than loops.

Now, addressing your actual question, spolier alert, you can't get over the ==. But you can hide it using the eq function. You may use

df.eq(True).all()

Or,

df.where(df.eq(True), False).all()

Note that

df.where(df.eq(True), False)

       0
0   True
1  False

Which you may find useful if you want to convert non-"True" values to False for any other reason.




ANSWER 2

Score 3


I would actually use

(pd.DataFrame([True, 'a']) == True).all().item()

This way, you're checking for the value of the object, not just checking the "truthy-ness" of it.

This seems perfectly pythonic to me because you're explicitly checking for the value of the object, not just whether or not it's a truthy value.