正在检查数据帧组是否包含所提供列表中的子字符串(不区分大小写)



如何根据另一列的值打印/返回值?

df = my_df[['Index', 'FRUITS']]
print(df)
Index            FRUITS
7       Green Apple
7             Mango
7            Orange
7        Strawberry
9         Pineapple
9            Banana
9            Grapes
10   Orange (Unripe)
10              Plum
L = ['apple', 'orange']

在这里,我想检查字符串Apple橙色是否存在于子字符串,无论大小写,每个索引都将返回未找到这两种水果之一的序列号

我尝试使用不同答案的方法,并尝试分组和迭代结果:

out = df.groupby('Index')['FRUITS'].apply(lambda x: L in x)
TypeError: 'in <string>' requires string as left operand, not list

因此,预期输出为:

[9, 10]

使用str.findall

result = df.groupby('Index')['FRUITS'].apply(' '.join).str.lower().str.findall('\bapple\b|\borange\b').str.len() < 2
list(result[result].index)

[9, 10]

最新更新