Skip to main content
 首页 » 编程设计

python之根据列表中的值过滤数据框

2025年05月04日13jirigala

我有以下数据框:-

我想过滤掉 claim_status 中有 11 的地方

以及 aa1 的 claim_ststaus_reason。

我正在尝试下面的代码,但它只是给了我所有的行

my_list = 'aa1' 
 
df[df['claim_status_reason'].str.contains( "|".join(my_list), regex=True)].reset_index(drop=True) 

预期输出:-

1.) where there is 11 in claim_ststus  
2.) where there is aa1 in the claim_status_reason 

请您参考如下方法:

您可以使用 apply 来获取您想要的过滤器,例如:

df[(df['claim_staus'].apply(lambda x: 11 in x)) & (df['claim_status_reason'].apply(lambda x: 'a1' in x))]