The Python Oracle

How to check if all values in a dataframe are True

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2 Looping

--

Chapters
00:00 How To Check If All Values In A Dataframe Are True
00:38 Answer 1 Score 3
00:59 Accepted Answer Score 18
01:36 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.