The Python Oracle

Inconsistent behavior of any(df == value) on pandas dataframe

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: A Thousand Exotic Places Looping v001

--

Chapters
00:00 Inconsistent Behavior Of Any(Df == Value) On Pandas Dataframe
00:53 Accepted Answer Score 6
01:44 Answer 2 Score 2
01:55 Answer 3 Score 3
02:05 Thank you

--

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

--

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

--

Tags
#python #pandas #any

#avk47



ACCEPTED ANSWER

Score 6


You need to use pandas built in any instead of any from base Python:

df1.eq(1).any().any()
# True

df2.eq(1).any().any()
# True

When using any from python, it treats the data frame as an iterable/dictionary and thus only check the column names, without looking at the values of the data frame; If you simply loop through df1 and df2, you can see it only returns the column names, which is how a dictionary behaves; Since df1 contains column names of 0 and 1, any([0,1]) will return True; df2, on the other hand, contains only one column of [0], any([0]) returns False. So any(df == 1) is somewhat equivalent to any(df) or any(df.columns):

[x for x in df1]
# [0, 1]

[x for x in df2]
# [0]



ANSWER 2

Score 3


In pandas better use DataFrame.any.

Numpy solutions:

print ((df1 == 1).values.any())
True
print ((df2 == 1).values.any())
True



ANSWER 3

Score 2


You need to use (df2 == 1).any() instead