這似乎是一個簡單的問題,但我之前找不到它(this和this很接近,但答案不是很好)。問題是:如果我想在我的 df某處搜索一個值(我不知道它在哪一列)并返回所有匹配的行。最 Pandaic 的方法是什么?有什么比:for col in list(df): try: df[col] == var return df[df[col] == var] except TypeError: continue ?
3 回答

慕婉清6462132
TA貢獻1804條經驗 獲得超2個贊
你應該使用isin,這是返回列,是想要行檢查冷'答案:-)
df.isin(['bal1']).any()
A False
B True
C False
CLASS False
dtype: bool
或者
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
添加回答
舉報
0/150
提交
取消