Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do word count on pandas dataframe [duplicate]

Tags:

python

pandas

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
like image 689
Nabih Bawazir Avatar asked Dec 24 '22 01:12

Nabih Bawazir


1 Answers

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
like image 113
jezrael Avatar answered Jan 06 '23 01:01

jezrael