python-3.x 使用值列表作为参数过滤 Dataframe [duplicate]

toe95027  于 2022-12-05  发布在  Python
关注(0)|答案(2)|浏览(149)

此问题在此处已有答案

Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()(13个答案)
Filtering Pandas Dataframe using OR statement(3个答案)
21小时前关门了。
我有这样的 Dataframe :

id   check_id
1       10
1       100
2       10
3       34
4       12
1       101

和列表:

list=[10,101]

我尝试过滤这个df,如下所示:

df[(df['id']==1) and (df['check_id'].isin(list))]

要获得此输出,请执行以下操作:

id   check_id
1       10
1       101

但我收到此错误消息:

The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我一直在尝试解决这个问题,但到目前为止还没有成功。
我该如何修复它?

zhte4eai

zhte4eai1#

如果你搜索而不是问问题,你会很容易得到答案

df[(df['id']==1) & (df['check_id'].isin(list))]
b1payxdu

b1payxdu2#

参照this article,查询应为:

df[(df['id']==1) & (df['check_id'].isin(list))]

相关问题