相关疑难解决方法(0)

计算每行的单词数

我正在尝试在数据框中创建一个新列,其中包含相应行的字数.我正在寻找单词的总数,而不是每个不同单词的频率.我认为会有一个简单/快速的方法来做到这一点共同的任务,但周围的Googling和阅读SO职位(一小撮后1,2,3,4)我卡住了.我已经尝试了在链接的SO帖子中提出的解决方案,但是回到了很多属性错误.

words = df['col'].split()
df['totalwords'] = len(words)
Run Code Online (Sandbox Code Playgroud)

结果是

AttributeError: 'Series' object has no attribute 'split'
Run Code Online (Sandbox Code Playgroud)

f = lambda x: len(x["col"].split()) -1
df['totalwords'] = df.apply(f, axis=1)
Run Code Online (Sandbox Code Playgroud)

结果是

AttributeError: ("'list' object has no attribute 'split'", 'occurred at index 0')
Run Code Online (Sandbox Code Playgroud)

python string dataframe python-3.x pandas

14
推荐指数
3
解决办法
5585
查看次数

标签 统计

dataframe ×1

pandas ×1

python ×1

python-3.x ×1

string ×1