How to search a partial String in the whole dataframe using pandas?

Viewed 4663

How to search for a string value on each and every columns using pandas . Lets say I have 32 columns ,

df[df['A'].str.contains("hello")]

this returns whether the value is present in "A" column or not ,How to search on every columns and the row in which the value is exist . Dataset :

A           B           C
1           hi          hie
2           bye         Hello

If I search for "hello" or "Hello" output should be :

A           B            C
2           bye         Hello
3 Answers

A way to check for existence of partial string, if you need entire string it will be faster than this:

df.stack().str.contains('search_string').sum()>0

However, to answer your question. Convert the DataFrame into Series by stacking all columns. Then search the Series for the string of interest. From the resulting matches, get the row index that is the same as it was in the DataFrame. Using the row index look up the rows from the DataFrame. I think this method is at least 20% faster than applymap method.

s = df.stack() # convert entire data frame into a series of values df.iloc[s[s.str.contains('search_string',na=False)].index.get_level_values(0)]

Related