如何筛选出包含特定字符串的“set”类型值的数据行?

2024-06-26 10:48:13 发布

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

我有一些dataframe,其中包含一个值为“set”类型的列

我还有一个我希望在这些集合中搜索的单词列表,并删除包含该列表中的热门单词的行

例如df结构

id   types 
123  {'Editorial', "Research Support, Non-U.S. Gov't", 'Comment'}
234  {'Comparative Study', 'Journal Article', "Research Support,'Research Support, N.I.H., Extramural'}

这是我要删除的值列表

list_to_drop=['Editorial','Comment']

在本例中,我希望删除第一行

谢谢


Tags: id类型supportdataframedf列表comment单词
3条回答

您可以将mapissubset一起使用:

df[~df.types.map(set(list_to_drop).issubset)]

map中使用^{}^{}过滤器:

df = df[df['types'].map(set(list_to_drop).isdisjoint)]
print (df)
    id                                              types
1  234  {Comparative Study, Research Support, N.I.H., ...

将以下代码与applydifference一起使用:

df['types'] = df['types'].apply(lambda x: x.difference(list_to_drop))

相关问题 更多 >