Blaszard
Blaszard

Reputation: 31955

Check if one value exists in any rows of any columns in pandas?

Is there any function to check if a value exists in any rows of any columns in pandas, such as

columnA columnB columnC
"john" 3 True
"mike" 1 False
"bob" 0 False

on the dataframe above, I want to know if there are any values named "mike" in any elements of the whole dataframe, and if it exists, I'd like to get True - otherwise get False.

Thanks.

Upvotes: 4

Views: 12110

Answers (1)

roman
roman

Reputation: 117337

Something like this:

df.apply(lambda x: 'mike' in x.values, axis=1).any()

or

df.applymap(lambda x: x == 'mike').any().any()

Upvotes: 15

Related Questions