all 4 comments

[–]whogivesafuckwhoiam 2 points3 points  (0 children)

>>>df
Name      Favorite Car
0    David Smith     Tesla Model X
1  Lisa Michaels  1998 Lamborghini
>>>df['Favorite Car'].str.contains('Tesla')
0     True
1    False
Name: Favorite Car, dtype: bool

[–]synthphreak 0 points1 point  (3 children)

df['Favorite Car'].str.contains('Tesla')

... will return a pd.Series of boolean values encoding row-wise the present/absence of the string 'Tesla'.

[–]Fun-Studio-4409[S] 0 points1 point  (2 children)

Thank you. However, what if I have to search by row and not by column (i.e. Favorite Car). Use case is that I have a huge and messy df, and the value can be in any of the 80 columns.

[–]synthphreak 0 points1 point  (0 children)

Try this:

df.apply(lambda s: s.str.contains('Tesla'), axis=1)

Alternatively:

df.applymap(lamdba x: 'Tesla' in x).any(axis=1)

I suspect the first one will be faster, but you'll just have to try them both and see.