根据lis中的值筛选数据帧

2024-10-01 22:38:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下数据框:-

enter image description here

我想过滤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

Tags: 数据intruedfismystatus地方
2条回答

不要对序列中的列表使用字符串操作。你可以用列表理解代替。您的数据结构选择是反熊猫的,因为您首先应该尽量避免将列表串联起来。这些操作不可矢量化。你知道吗

mask1 = np.array([11 in x for x in df['claim_staus']])
mask2 = np.array(['aa1' in x for x in df['claim_status_reason']])

df = df[mask1 & mask2]

您可以使用apply获得所需的过滤器,如:

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

相关问题 更多 >

    热门问题