Count number of words per row Count number of words per row python-3.x python-3.x

Count number of words per row


str.split + str.len

str.len works nicely for any non-numeric column.

df['totalwords'] = df['col'].str.split().str.len()

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()]


Here is a way using .apply():

df['number_of_words'] = df.col.apply(lambda x: len(x.split()))

example

Given this df:

>>> df                    col0  This is one sentence1           and another

After applying the .apply()

df['number_of_words'] = df.col.apply(lambda x: len(x.split()))>>> df                    col  number_of_words0  This is one sentence                41           and another                2

Note: As pointed out by in comments, and in this answer, .apply is not necessarily the fastest method. If speed is important, better go with one of @cᴏʟᴅsᴘᴇᴇᴅ's methods.


This is one way using pd.Series.str.split and pd.Series.map:

df['word_count'] = df['col'].str.split().map(len)

The above assumes that df['col'] is a series of strings.

Example:

df = pd.DataFrame({'col': ['This is an example', 'This is another', 'A third']})df['word_count'] = df['col'].str.split().map(len)print(df)#                   col  word_count# 0  This is an example           4# 1     This is another           3# 2             A third           2