Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python pandas dataframe - conditional group by

My question is simple, I have a dataframe as follows:

   Name  Meal
0  John  Chicken
1  John  Chicken
2  John  Pizza
3  Paul  Chicken
4  Paul  Pizza
5  Paul  Pizza
6  Jack  Chicken
7  Jack  Chicken
8  Jack  Fish

I want to somehow conditionally group by to create new series in a new dataframe that specify the count of each meal type, e.g.:

   Name  Chicken_meals  Pizza_meals  Fish_meals
0  John  2              1            0
1  Paul  1              2            0
2  Jack  2              0            1

This is my first question so apologies if the formatting isn't perfect - I tried my best! Thanks in advance!

like image 598
Brad Avatar asked Jun 07 '26 20:06

Brad


1 Answers

Answer updated after stealing heavily from the great ideas in @Baron Legendre answer

This can be done via pivot_table:

dfp = pd.pivot_table(df,index='Name', columns='Meal', fill_value=0, aggfunc=len) \
        .add_suffix('_meals').reset_index().rename_axis(None, axis=1) 

print(dfp)

Result

   Name  Chicken_meals  Fish_meals  Pizza_meals
0  Jack              2           1            0
1  John              2           0            1
2  Paul              1           0            2
like image 200
sitting_duck Avatar answered Jun 10 '26 17:06

sitting_duck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!