计算来自 pandas 数据框的不同单词

编程入门 行业动态 更新时间:2024-10-26 13:28:06
本文介绍了计算来自 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 数据框的不同单词

本文发布于:2023-05-27 09:58:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/285798.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单词   数据   pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!