熊猫数据框-如何查找每行重复的单词

时间:2020-09-27 08:15:19

标签: python pandas dataframe

我有一个数据框,其中的文本列包含长字符串值。文本已清除,只有单词,如下面的示例所示。

text
=====
This is the first row
This is the second row
third row this is the 

我想获得此内容:

text
=====
first
second
third

如何删除数据框每一行中出现的单词?

import pandas as pd

df = pd.DataFrame({'text': ['This is the first row','This is the second row', 'third row this is the']})

# what next?

1 个答案:

答案 0 :(得分:0)

将数据框转换为字符串,然后您可以执行以下操作:

text = 'This is the first row, This is the second row, This is the third row'
arr = [set(x.split()) for x in text.split(',')]
mutual_words = set.intersection(*arr)
result = [list(x.difference(mutual_words)) for x in arr]
result = sum(result, [])
final_text = (", ").join(result)
print(final_text)
# 'first, second, third'
相关问题