'Pandas - find specific value in entire dataframe
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.
Solution 1:[1]
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.
Solution 2:[2]
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']
Solution 3:[3]
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.
Solution 4:[4]
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)]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Vijay Anand Pandian |
Solution 2 | Pratik Patel |
Solution 3 | Mayank Porwal |
Solution 4 | marc_s |