首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >熊猫数据-如何找到每行重复的单词

熊猫数据-如何找到每行重复的单词
EN

Stack Overflow用户
提问于 2020-09-27 08:15:19
回答 1查看 684关注 0票数 0

我有一个包含长字符串值的文本列的dataframe。如下面的例子所示,文本已经被清除,并且只有单词。

代码语言:javascript
运行
复制
text
=====
This is the first row
This is the second row
third row this is the 

我想得到以下内容:

代码语言:javascript
运行
复制
text
=====
first
second
third

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

代码语言:javascript
运行
复制
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?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-27 08:54:21

将dataframe转换为字符串,然后可以执行以下操作:

代码语言:javascript
运行
复制
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'
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64086312

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档