Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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.

like image 455
Blaszard Avatar asked Sep 19 '25 00:09

Blaszard


1 Answers

Something like this:

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

or

df.applymap(lambda x: x == 'mike').any().any()
like image 70
Roman Pekar Avatar answered Sep 21 '25 14:09

Roman Pekar