我正试图找到一种更有效地完成这个过程的方法。由于我的数据帧有大约10万行,而每行最多可包含20k个字符串。
如果该单词在另一个列表中,我想从列表中删除单词。除了我的数据有多大,我的删除列表大小约为600k。
我希望有一种矢量化解决方案,但不确定它是否可行。
我现在正在做什么
removelist = df2.words.tolist()
for row in df.itertuples():
df.at[row.Index, 'tweet'] = [x for x in row.tweet if x not in removelist]
我知道我可以将它们转换为一组并且可以
set(row.tweet).intersection(screen)
但保持重复是非常重要的。谁能指出我正确的方向?
编辑:示例数据
df
tweet user
0 [@a] 1
1 [@b] 2
2 [#c, #d, #e, #f, #e] 3
3 [@g] 4
df2
words
0 #d
1 @a
期望的输出:
tweet user
0 [] 1
1 [@b] 2
2 [#c, #e, #f, #e] 3
3 [@g] 4
迭代itertuples
很慢。我建议使用列表理解来获得最大速度(因为这不是你可以矢量化的操作,这可能是你最好的选择):
removeset = set(df2.words.tolist())
df['tweet'] = [
[j for j in i if j not in removeset] for i in df.tweet.tolist()
]