【发布时间】:2015-10-20 16:16:56
【问题描述】:
我正在尝试计算我的数据框列中的单个单词。它看起来像这样。实际上,这些文本是推文。
text
this is some text that I want to count
That's all I wan't
It is unicode text
所以我从其他 stackoverflow 问题中发现,我可以使用以下内容:
Count most frequent 100 words from sentences in Dataframe Pandas
Count distinct words from a Pandas Data Frame
我的 df 被称为结果,这是我的代码:
from collections import Counter
result2 = Counter(" ".join(result['text'].values.tolist()).split(" ")).items()
result2
我收到以下错误:
TypeError Traceback (most recent call last)
<ipython-input-6-2f018a9f912d> in <module>()
1 from collections import Counter
----> 2 result2 = Counter(" ".join(result['text'].values.tolist()).split(" ")).items()
3 result2
TypeError: sequence item 25831: expected str instance, float found
文本的 dtype 是对象,据我了解,这对于 unicode 文本数据是正确的。
【问题讨论】:
-
显然你的数据框中有浮点值,你想用它们做什么?你也想数一数吗?
-
因为这些文本应该都是推文,所以我也想计算它们。如果此列还包含浮点值,这是否意味着我收集的推文只是数字? (让我好奇哪些是浮动的)
-
是的,这是可能的。