How to count specific words in each row of a column python pandas

Question:

I have the following dataframe in pandas:

test = pd.DataFrame({'Food': ['Apple Cake Apple', 'Orange Tomato Cake', 'Broccoli Apple Orange', 'Cake Orange Cake', 'Tomato Apple Orange'], 'Type' : ['Fruit Dessert', 'Fruit Veggie', 'Veggie Fruit', 'Dessert Fruit', 'Veggie Fruit']})
test

          Food                    Type
0   Apple Cake Apple       Fruit Dessert Fruit
1   Orange Tomato          Fruit Veggie Dessert
2   Broccoli Apple Orange  Veggie Fruit Fruit
3   Cake Orange Cake       Dessert Fruit Dessert
4   Tomato Apple Orange    Veggie Fruit Fruit

I would like to make a new column that counts the values in the “Type” column and orders them in greatest to least regardless of the type of food. For example, this would be exactly what I’m looking for:

test = pd.DataFrame({'Food': ['Apple Cake Apple', 'Orange Tomato Cake', 'Broccoli Apple Orange', 'Cake Orange Cake', 'Tomato Apple Orange'],
                     'Type' : ['Fruit Dessert Fruit', 'Fruit Veggie Dessert', 'Veggie Fruit Fruit', 'Dessert Fruit Dessert', 'Veggie Fruit Fruit'],
                     'Count': ['2 1', '1 1 1 ', '2 1', '2 1', '2 1']})
test

    Food                             Type          Count
0   Apple Cake Apple        Fruit Dessert Fruit     2 1
1   Orange Tomato Cake      Fruit Veggie Dessert    1 1 1
2   Broccoli Apple Orange   Veggie Fruit Fruit      2 1
3   Cake Orange Cake        Dessert Fruit Dessert   2 1
4   Tomato Apple Orange     Veggie Fruit Fruit      2 1

How would I go about doing this? Thanks so much!

Asked By: bismo

||

Answers:

s=test.Type.str.split().explode()
s=s.groupby([s.index,s]).size().sort_values(ascending=False).groupby(level=0).agg(lambda x : ' '.join(x.astype(str)))
df['C']=s
0      2 1
1    1 1 1
2      2 1
3      2 1
4      2 1
Name: Type, dtype: object
Answered By: BENY