这似乎是一个简单的问题,但我之前找不到它(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
提交
取消