0

After performing the groupby on two columns (id and category) using the mean aggregation function over a column (col3) I have something like this:

                              col3
id            category        mean
345              A             12
                 B              2
                 C              3
                 D              4
                Total          21

What I would like to do is to add a new column called percentage in which I calculate the percentage of each category over the category Total. This should be done separately for every id. The result should be something like this:

                              col3
id            category        mean    percentage
345              A             12        0.57
                 B              2        0.09
                 C              3        0.14
                 D              4        0.19
                Total          21         1

Obviously i want to do that for every id, that is the first column on which i have done the groupby. Any suggestion on how to do that?

2 Answers 2

1

Using get_level_values filter your df, then we using div

s=df[df.index.get_level_values(level=1)!='Total'].sum(level=0)
df['percentage']=df.div(s,level=0,axis=1)
df
Out[422]: 
              mean  percentage
id  category                  
345 A           12    0.571429
    B            2    0.095238
    C            3    0.142857
    D            4    0.190476
    Total       21    1.000000
Sign up to request clarification or add additional context in comments.

Comments

0

That's my suggestion:

df['mean'] = df['mean'] / df['mean'].sum()

1 Comment

This is not working because I need to do that for every ID separately. In this way instead you consider the whole dataset

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.