Skip to content Skip to sidebar Skip to footer

Seaborn And Pandas: Make Multiple X-category Bar Plot Using Multi Index Data In Python

I have a multi-index dataframe that I've melted to look something like this: Color Frequency variable value Red 2-3 times a month x 22 Red

Solution 1:

This is what you need to find the portion of each number for that group:

df['proportion'] = df['value'] / df.groupby(['Color','variable'])['value'].transform('sum')

Output:

   variable              Frequency Color  value   portion
0         x      2-3 times a month   Red     220.0522571         x      A few days a week   Red     450.1068882         x    At least once a day   Red    3440.8171023         x                  Never   Red      50.0118764         x           Once a month   Red      10.0023755         x            Once a week   Red      00.0000006         x  Once every few months   Red      40.0095017         x      2-3 times a month  Blue      40.0136058         x      A few days a week  Blue     490.1666679         x    At least once a day  Blue    2000.68027210        x                  Never  Blue      70.02381011        x           Once a month  Blue     190.06462612        x            Once a week  Blue     100.03401413        x  Once every few months  Blue      50.01700714        y      2-3 times a month   Red      30.01190515        y      A few days a week   Red     970.38492116        y    At least once a day   Red    1440.57142917        y                  Never   Red      40.01587318        y           Once a month   Red      00.00000019        y            Once a week   Red      00.00000020        y  Once every few months   Red      40.01587321        y      2-3 times a month  Blue     440.09977322        y      A few days a week  Blue     620.14059023        y    At least once a day  Blue    3000.68027224        y                  Never  Blue      20.00453525        y           Once a month  Blue      40.00907026        y            Once a week  Blue     230.05215427        y  Once every few months  Blue      60.01360528        z      2-3 times a month   Red      40.03149629        z      A few days a week   Red     120.09448830        z    At least once a day   Red    1010.79527631        z                  Never   Red      00.00000032        z           Once a month   Red      00.00000033        z            Once a week   Red     100.07874034        z  Once every few months   Red      00.00000035        z      2-3 times a month  Blue    1000.11037536        z      A few days a week  Blue    2030.22406237        z    At least once a day  Blue    2990.33002238        z                  Never  Blue      00.00000039        z           Once a month  Blue      00.00000040        z            Once a week  Blue    2040.22516641        z  Once every few months  Blue    1000.110375

Post a Comment for "Seaborn And Pandas: Make Multiple X-category Bar Plot Using Multi Index Data In Python"