Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count top-3 words used in column and store result in dictionary

Tags:

python

pandas

I currently have a dataframe like this:

column_1:   column_2:    column_3:
  pizza       beer         nice, excellent, good
  pasta       beer         good, nice, great
  pizza       wine         great, nice
  fish        coffee       ok

I am trying to get the top-3 words that occur in column_3 and store them into a dictionary.

My expected output:

{ 'nice': 3,
  'good': 2,
  'great':2 }

What is the best way to do this? Or is it even possible?

Any help is much appreciated.

like image 891
Singhpratk44 Avatar asked Jan 27 '23 15:01

Singhpratk44


1 Answers

Using get_dummies + nlargest

d=df['column_3:'].str.get_dummies(',').sum().nlargest(3).to_dict()
d
Out[225]: {'good': 2, 'great': 2, 'nice': 3}
like image 93
BENY Avatar answered Jan 29 '23 05:01

BENY