从熊猫数据框列中的列表中删除元素的最有效的方法

时间:2020-03-05 10:31:36

标签: python pandas

我有一个带有一列列表和一个带有一些选定单词的集合的数据框:

>>>df
      column1
1    ["some", "strings"]
2    ["words", "group of words"]
3    ["more", "words"]

>>>special_words
{"some", "strings", "more"}

我想要的输出是

>>>df
      column1
1    ["some", "strings"]
2    []
3    ["more"]

我现在使用的代码在速度方面表现良好。但是,由于删除了许多元素,我希望内存使用量会下降,但是这种情况不会发生,并且内存使用量会更高。有人可以解释为什么会这样吗或提出替代代码吗?

# How I am doing right now
df["column1"] = [[x for x in row if x in special_words] for row in df["column1"]]

0 个答案:

没有答案