Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if all values in a dataframe are True

Tags:

python

pandas

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

like image 834
MLguy Avatar asked Mar 26 '18 18:03

MLguy


People also ask

How do I know if a DataFrame is true?

all() method checks whether all elements are True, potentially over an axis. It returns True if all elements within a series or along a Dataframe axis are non-zero, not-empty or not-False.

How do you check if a DataFrame has a value?

You can check if a column contains/exists a particular value (string/int), list of multiple values in pandas DataFrame by using pd. series() , in operator, pandas.


2 Answers

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.

like image 150
cs95 Avatar answered Oct 04 '22 03:10

cs95


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.

like image 32
wpercy Avatar answered Oct 04 '22 03:10

wpercy