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
?
You can check if a column contains/exists a particular value (string/int), list of multiple values in pandas DataFrame by using pd. series() , in operator, pandas. series. isin() , str.
In Pandas, DataFrame. loc[] property is used to get a specific cell value by row & label name(column name).
isin() function check whether values are contained in Series. It returns a boolean Series showing whether each element in the Series matches an element in the passed sequence of values exactly.
You can perform equality comparison on the entire DataFrame:
df[df.eq(var1).any(1)]
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With