TheRealFakeNews
TheRealFakeNews

Reputation: 8153

Pandas: How can I check if a pandas dataframe contains a specific value?

Specifically, I want to see if my pandas dataframe contains a False.

It's an nxn dataframe, where the indices are labels.

So if all values were True except for even one cell, then I would want to return False.

Upvotes: 3

Views: 1297

Answers (2)

Attila Tanyi
Attila Tanyi

Reputation: 5044

If you just want to scan your whole dataframe looking for a value, check df.values. It returns an array of all the rows in the dataframe.

value = False  # This is the value you're searching
df_contains_value = any([value in row for row in df.values])

Upvotes: 0

ℕʘʘḆḽḘ
ℕʘʘḆḽḘ

Reputation: 19375

your question is a bit confusing, but assuming that you want to know whether there is at least one False in your dataframe, you could simply use

mask = df.mycol == False
mask.value_counts()
mask.sum()
mask.sum() > 0

All will tell you the truth

Upvotes: 1

Related Questions