如何过滤掉包含'set'类型中包含某些字符串的值的熊猫 df 行?



我有一些数据帧,其中包含一个带有"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']

在这个例子中,我希望删除第一行

谢谢!

map中按boolean indexing过滤使用isdisjoint

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))

你可以使用 map with issubset:

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

最新更新