38

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 

?

cs95
  • 330,695
  • 80
  • 606
  • 657
Josh Friedlander
  • 8,209
  • 4
  • 32
  • 67

3 Answers3

53

You can perform equality comparison on the entire DataFrame:

df[df.eq(var1).any(1)]
cs95
  • 330,695
  • 80
  • 606
  • 657
28

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
BENY
  • 296,997
  • 19
  • 147
  • 204
2

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("")
sɐunıɔןɐqɐp
  • 2,877
  • 15
  • 33
  • 38
ajay singh
  • 39
  • 2