Q:

percentage count for pandas df grouped by multiple columns

I have two pandas df. The data is grouped by month, category, product. It also has a spend column. I need to calculate the percentage of spend column. Below is the sample of df_raw:
                                         spend_sum
category     month     product_list
Home          1         A                    10
                        B                    20
                        C                    30

Home          2         A                    40
                        B                    50
                        C                    60

Below is the sample of df_new:
                                         spend_sum
category     month     product_list
Home          1         A                    1
                        B                    2
                        C                    3

Home          2         A                    20
                        B                    10
                        C                    5

My code is:
df_raw = df.explode('product_list').groupby(['category', 'month', 'product_list']).count()

I need to divide df_new['spend_sum'] / df_raw['spend_sum']
Desired output is:
                                         percentage
category     month     product_list
Home          1         A                    0.1
                        B                    0.1
                        C                    0.1

Home          2         A                    0.5
                        B                    0.2
                        C                    0.008

A:

Just this will do:
df_new['pct'] = df_new['spend_sum']/df_raw['spend_sum']

                             spend_sum       pct
category month product_list
Home     1     A                     1  0.100000
               B                     2  0.100000
               C                     3  0.100000
         2     A                    20  0.500000
               B                    10  0.200000
               C                     5  0.083333

