问题
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