4

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.

Blaszard
  • 30,954
  • 51
  • 153
  • 233

1 Answers1

15

Something like this:

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

or

df.applymap(lambda x: x == 'mike').any().any()
Roman Pekar
  • 107,110
  • 28
  • 195
  • 197
  • The latter is substantially faster. df.applymap(lambda x: x == 'mike').any().any() took under 4 seconds on 283900 rows × 30 columns while df.apply(lambda x: 'mike' in x.values, axis=1).any() took over 7 minutes. – John Anderson Aug 19 '22 at 20:02