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')
str.len works nicely for any non-numeric column.
df['totalwords'] = df['col'].str.split().str.len()
If your words are single-space separated, you may simply count the spaces plus 1.
df['totalwords'] = df['col'].str.count(' ') + 1
This is faster than you think!
df['totalwords'] = [len(x.split()) for x in df['col'].tolist()]