Elrond
Elrond

Reputation: 2112

How to check if all values in a dataframe are True

pd.DataFrame.all and pd.DataFrame.any convert to bool all values and than assert all identities with the keyword True. This is ok as long as we are fine with the fact that non-empty lists and strings evaluate to True. However let assume that this is not the case.

>>> pd.DataFrame([True, 'a']).all().item()
True  # Wrong

A workaround is to assert equality with True, but a comparison to True does not sound pythonic.

>>> (pd.DataFrame([True, 'a']) == True).all().item()
False  # Right

Question: can we assert for identity with True without using == True

Upvotes: 10

Views: 25677

Answers (2)

cs95
cs95

Reputation: 402263

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.

Upvotes: 19

wpercy
wpercy

Reputation: 10090

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.

Upvotes: 3

Related Questions