计算每行字数 [英] Count number of words per row
问题描述
我正在尝试在数据框中创建一个新列,其中包含相应行的字数统计。我希望查看的是单词总数,而不是每个不同单词的出现频率。我以为有一种简单/快速的方法可以完成这项常见任务,但是在谷歌搜索并阅读了一些SO帖子之后( 1 , 2 , 3 , 4 )我被卡住了。我已经尝试了在链接的SO帖子中提出的解决方案,但又收到了很多属性错误。
I'm trying to create a new column in a dataframe that contains the word count for the respective row. I'm looking to the total number of words, not frequencies of each distinct word. I assumed there would be a simple/quick way to do this common task, but after googling around and reading a handful of SO posts (1, 2, 3, 4) I'm stuck. I've tried the solutions put forward in the linked SO posts, but get lots of attribute errors back.
words = df['col'].split()
df['totalwords'] = len(words)
结果
AttributeError: 'Series' object has no attribute 'split'
和
f = lambda x: len(x["col"].split()) -1
df['totalwords'] = df.apply(f, axis=1)
结果
AttributeError: ("'list' object has no attribute 'split'", 'occurred at index 0')
推荐答案
< h3> str.split
+ str.len
str.len
可以很好地用于任何非数值列。
str.split
+ str.len
str.len
works nicely for any non-numeric column.
df['totalwords'] = df['col'].str.split().str.len()
str.count
如果您的单词用单空格分隔,则可以只需计算空格加1。
str.count
If your words are single-space separated, you may simply count the spaces plus 1.
df['totalwords'] = df['col'].str.count(' ') + 1
列表理解
List Comprehension
This is faster than you think!
df['totalwords'] = [len(x.split()) for x in df['col'].tolist()]
这篇关于计算每行字数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!