5

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
  • 6,381
  • 7
  • 37
  • 70
  • 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

11

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
  • 822,522
  • 95
  • 1,334
  • 1,252
3

You can try:

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