我正在尝试删除每行数据框中字符串中的重复单词。
说我的数据框看起来像这样:
In:
Yes Yes Absolutely
No No Nope
Win Win Lose
for row in df.iterrows():
row["Sentence"] = (list(set(row["Sentence"])))
Desired Out:
Yes Absolutely
No Nope
Win Lose
如何清除每一行以删除重复的字符串。我已经尝试了上面的代码。
任何指向任何文档或资源的链接,如果可以引导我朝正确的方向前进,将不胜感激。谢谢。
您可以使用(假设列名称为0):
from collections import OrderedDict
df[0].str.split().apply(lambda x: ','.join(OrderedDict.fromkeys(x).keys()))
0 Yes,Absolutely
1 No,Nope
2 Win,Lose
注意,您可以将set用作:
df[0].str.split().apply(lambda x: ','.join(list(set(x))))
但是set不能保证顺序。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句