12

I have a dataframe and I want to search all columns for values that is text 'Apple'. I know how to do it with one column, but how can I apply this to ALL columns? I want to make it a function, so that next time I can directly use it to search for other values in other dateframes.

Thanks.

Mayank Porwal
  • 31,737
  • 7
  • 30
  • 50
Chloe
  • 147
  • 1
  • 1
  • 5

5 Answers5

12

you can try searching entire dataframe using the below code

df[df.eq("Apple").any(1)]

Using numpy comparison

df[(df.values.ravel() == "Apple").reshape(df.shape).any(1)]

Both are faster smaller records but not sure about large dataset.

11

import pandas library

import pandas as pd

Raw Data or URL of file

raw_data = {'first_name': ['Mihir', 'Mihir', 'Raju', 'Johan', 'Johan'],
               'last_name': ['Patel', 'Patel', 'Ali', 'Khan', 'Khan'], 
               'age': [42, 42, 36, 24, 53]}
df = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age'])

Find The Value

df.loc[df['first_name']=='Mihir']
Pratik Patel
  • 303
  • 3
  • 6
  • 6
    This finds the value from a single column. The OP specifically asked for a solution that searched the entire df. – ViennaMike Dec 12 '21 at 03:01
8

Something like this:

In [1188]: df
Out[1188]: 
   id   name      n1
0   1   Zeke     may
1   2  Apple    maya
2   3      a   Apple
3   4   Maya       a
4   5  Derek  Mayank
5   6     an      is
6   7    the     the

Just have a check like:

In [1190]: df[df == 'Apple']
Out[1190]: 
   id   name     n1
0 NaN    NaN    NaN
1 NaN  Apple    NaN
2 NaN    NaN  Apple
3 NaN    NaN    NaN
4 NaN    NaN    NaN
5 NaN    NaN    NaN
6 NaN    NaN    NaN

OR

In [1191]: df.where(df == 'Apple')
Out[1191]: 
   id   name     n1
0 NaN    NaN    NaN
1 NaN  Apple    NaN
2 NaN    NaN  Apple
3 NaN    NaN    NaN
4 NaN    NaN    NaN
5 NaN    NaN    NaN
6 NaN    NaN    NaN

This lets you search through all the columns of a dataframe.

Mayank Porwal
  • 31,737
  • 7
  • 30
  • 50
0

Search list of values in given dataframe all columns:

filter_data = df[df.isin(['Apple', 'Green Apple']).any(1)]

Search single value in given dataframe all columns:

filter_data = df[df.contains('Apple').any(1)]
marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
atiwari
  • 11
  • 2
  • Are you refering to https://pandas.pydata.org/docs/reference/api/pandas.Series.str.contains.html ? Because dataframe does not have any "contains" method – Tito Rezende May 11 '22 at 01:19
-6

Take what you have for your single method and include in the for loop.

for column in df:
    #your code here