且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 Pandas 数据框中计算不同的单词

更新时间:2023-11-23 15:56:28

使用 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'])

或与评论中的 Counter() 一起使用:

Or use with Counter() from comments:

from collections import Counter
results = Counter()
df['text'].str.lower().str.split().apply(results.update)
print(results)