简体   繁体   English

绘制Pandas多索引DataFrame或系列的难度

[英]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) 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. 例如,我将在我所拥有的不同会话期间绘制0-17岁,18-24岁,25-29岁年龄组的男性和女性用户。

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 ] 2

I think you can use unstack with DataFrame.plot.bar : 我想你可以使用unstackDataFrame.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()

图形

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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