Search for a value anywhere in a pandas DataFrame

Question:

This seems like a simple question, but I couldn’t find it asked before (this and this are close but the answers aren’t great).

The question is: if I want to search for a value somewhere in my df (I don’t know which column it’s in) and return all rows with a match.

What’s the most Pandaic way to do it? Is there anything better than:

for col in list(df):
    try:    
        df[col] == var
        return df[df[col] == var]
    except TypeError:
        continue 

?

Asked By: Josh Friedlander

||

Answers:

You should using isin , this is return the column , is want row check cold’ answer 🙂

df.isin(['bal1']).any()
A        False
B         True
C        False
CLASS    False
dtype: bool

Or

df[df.isin(['bal1'])].stack() # level 0 index is row index , level 1 index is columns which contain that value 
0  B    bal1
1  B    bal1
dtype: object
Answered By: BENY

You can perform equality comparison on the entire DataFrame:

df[df.eq(var1).any(1)]
Answered By: cs95

You can try the code below:

import pandas as pd
x = pd.read_csv(r"filePath")
x.columns = x.columns.str.lower().str.replace(' ', '_')
y = x.columns.values
z = y.tolist()
print("Note: It take Case Sensitive Values.")
keyWord = input("Type a Keyword to Search: ")
try:
    for k in range(len(z)-1):
        l = x[x[z[k]].str.match(keyWord)]
        print(l.head(10))
        k = k+1
except:
    print("")
Answered By: ajay singh

This is a solution which will return the actual column you need.

df.columns[df.isin(['Yes']).any()]
Answered By: anudeep99

Minimal solution:

import pandas as pd
import numpy as np

def locate_in_df(df, value):
    a = df.to_numpy()
    row = np.where(a == value)[0][0]
    col = np.where(a == value)[1][0]
    return row, col
Answered By: Rafael Fraga
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.