pandas dataframe- how to find words that repeat in each row [closed]

China☆狼群 提交于 2021-01-29 22:08:26

问题


I have a dataframe with a text column containing long string values. The text has been cleaned and has only words as shown in the example below.

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

I would like to get this content:

text
=====
first
second
third

How do I remove the words that appear in each row of a dataframe?

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:


Convert the dataframe to a string, then you can do something like this:

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'


来源:https://stackoverflow.com/questions/64086312/pandas-dataframe-how-to-find-words-that-repeat-in-each-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!