【发布时间】:2018-10-03 17:23:43
【问题描述】:
我正在尝试在数据框中创建一个新列,其中包含相应行的字数。我正在寻找单词的总数,而不是每个不同单词的频率。我以为会有一种简单/快速的方法来完成这项常见任务,但是在谷歌搜索并阅读了一些 SO 帖子(1、2、3、4)之后,我被卡住了。我已经尝试了链接的 SO 帖子中提出的解决方案,但得到了很多属性错误。
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')
【问题讨论】:
标签: python string python-3.x pandas dataframe