简体   繁体   中英

Difficulty in plotting Pandas Multi-indexed DataFrame or series

Please see this Image

s = pd.DataFrame(combined_df.groupby(['session','age_range', 'gender']).size()) s.head(25)

​                        0
session   age_range gender        
Evening   0 - 17    female   31022
                    male     21754
          18 - 24   female   79086
                    male     71563
                    unknown     75
          25 - 29   female   29321
                    male     46125
                    unknown     44
          30 - 34   female   21480
                    male     25803
                    unknown     33
          35 - 44   female   17369
                    male     20335
                    unknown    121
          45 - 54   female    8420
                    male     12385
                    unknown     24
          55+       female    3433
                    male      9880
                    unknown    212
Mid Night 0 - 17    female   18456
                    male     12185
          18 - 24   female   50536
                    male     45829
                    unknown     62

This is how my Multi-indexed data Frame looks like. All I am trying to do is to plot the data in such a way that I can compare the male and female users of different age groups active during the different sessions(say Morning, Evening, Noon and Night). For example I will plot the Male and Female users of age group 0-17, 18-24, 25-29... during different Sessions that I have.

Note: I have tried a few examples from stack overflow and other websites still unsuccessful in getting what I need. So, I request you guys to try solving my problem and help me in finding a solution for this. I have been struggling with this for many days and even the documentation is vague. So, please throw some light on this problem. ] 2

I think you can use unstack with DataFrame.plot.bar :

import matplotlib.pyplot as plt
df = combined_df.groupby(['session','age_range', 'gender']).size()
df.unstack(fill_value=0).plot.bar()
plt.show()

图形

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM