本文介绍了计算来自 pandas 数据框的不同单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Pandas数据框,其中一列包含文本.我想获得一列出现在整列中的唯一单词的列表(空格是唯一的拆分).
I've a Pandas data frame, where one column contains text. I'd like to get a list of unique words appearing across the entire column (space being the only split).
import pandas as pd r1=['My nickname is ft.jgt','Someone is going to my place'] df=pd.DataFrame(r1,columns=['text'])输出应如下所示:
['my','nickname','is','ft.jgt','someone','going','to','place']获得计数也没有什么坏处,但这不是必需的.
It wouldn't hurt to get a count as well, but it is not required.
推荐答案使用set创建唯一元素的序列.
Use a set to create the sequence of unique elements.
对df进行一些清理,以小写并拆分字符串:
Do some clean-up on df to get the strings in lower case and split:
df['text'].str.lower().str.split() Out[43]: 0 [my, nickname, is, ft.jgt] 1 [someone, is, going, to, my, place]此列中的每个列表都可以传递给set.update函数以获取唯一值.使用 apply 进行此操作:
Each list in this column can be passed to set.update function to get unique values. Use apply to do so:
results = set() df['text'].str.lower().str.split().apply(results.update) print results set(['someone', 'ft.jgt', 'my', 'is', 'to', 'going', 'place', 'nickname'])更多推荐
计算来自 pandas 数据框的不同单词
发布评论