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:
JavaScript
x
7
1
for col in list(df):
2
try:
3
df[col] == var
4
return df[df[col] == var]
5
except TypeError:
6
continue
7
?
Advertisement
Answer
You can perform equality comparison on the entire DataFrame:
JavaScript
1
2
1
df[df.eq(var1).any(1)]
2