0

I have a data frame

Category  Group  Subgroup  Price
Ri          a       s        1
we          a       s        2
ge          a       c        4
re          b       f        1.5
mb          b       f        2.5

I want to know the number of unique category if I groupby the column Group and Subgroup.

for ex: The results should be like this if I group by Group and subgroup

Group  Subgroup  No_of_UniqueCategory Price
a        s              2             3
Amit
  • 763
  • 1
  • 5
  • 14
  • You can use .unique(), right? Post Groupby on the columns you are interested in and rename the columns if you want post that; – Aditya Mar 31 '20 at 04:28
  • you use a named aggregation: `df.groupby('Group').agg(Subgroup=('Subgroup','first'),No_of_UniqueCategory=('Subgroup','nunique'),Price=('Price','sum')).reset_index()` , change the grouper if you want to include Subgroup – anky Mar 31 '20 at 04:46

0 Answers0