4

I know this is may be silly, but every research I've done for this question is led to more complex questions, I still can't figure out the basics, I just want to count the frequency of words

Here's my data

id descriptions
1  I love you
2  I love you too

Here's my expected output

id descriptions      word count
1  I love you        3
2  I love you too    4
Nabih Bawazir
  • 5,275
  • 6
  • 29
  • 53
  • 1
    Identical duplicate of https://stackoverflow.com/questions/49984905/count-number-of-words-per-row/49984997#49984997 and likely many others. – cs95 Jan 28 '19 at 07:16

2 Answers2

7

Use:

df['count'] = df['descriptions'].str.count(' ') + 1

Or:

df['count'] = df['descriptions'].str.split().str.len()

Or:

df['count'] = df['descriptions'].str.findall(r'(\w+)').str.len()

print (df)
   id    descriptions  count
0   1      I love you      3
1   2  I love you too      4
jezrael
  • 729,927
  • 78
  • 1,141
  • 1,090
3

You can try:

df['word_count'] = df['description'].apply(lambda x: len(x.split())
Mohit Motwani
  • 4,322
  • 3
  • 17
  • 40