我有一个Pandas数据框,其中一列包含文本。我想获得一列出现在整列中的唯一单词的列表(空格是唯一的拆分)。
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']
计数也无济于事,但这不是必需的。
使用set
来创建唯一元素的序列。
进行一些清理df
以使小写字母的字符串分解:
df['text'].str.lower().str.split()
Out[43]:
0 [my, nickname, is, ft.jgt]
1 [someone, is, going, to, my, place]
该列中的每个列表都可以传递给set.update
函数以获取唯一值。使用apply
这样做:
results = set()
df['text'].str.lower().str.split().apply(results.update)
print(results)
set(['someone', 'ft.jgt', 'my', 'is', 'to', 'going', 'place', 'nickname'])
或与Counter()
from注释一起使用:
from collections import Counter
results = Counter()
df['text'].str.lower().str.split().apply(results.update)
print(results)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句