50

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
  • 379,657
  • 97
  • 704
  • 746
Josh Friedlander
  • 10,870
  • 5
  • 35
  • 75

5 Answers5

65

You can perform equality comparison on the entire DataFrame:

df[df.eq(var1).any(1)]
cs95
  • 379,657
  • 97
  • 704
  • 746
33

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
  • 317,841
  • 20
  • 164
  • 234
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
  • 3,332
  • 15
  • 36
  • 40
ajay singh
  • 39
  • 2
0

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

df.columns[df.isin(['Yes']).any()]
0

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
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 21 '23 at 01:33