简体   繁体   English

如何使用三列三行的三个数据框创建堆叠的条形图

[英]how to create a stacked bar with three dataframe with three columns & three rows

I have three dataframes df_Male , df_female , Df_TransGender 我有三个数据df_Male , df_female , Df_TransGender

sample dataframe df_Male 样本数据df_Male

continent   avg_count_country   avg_age
  Asia          55                5
  Africa        65                10
  Europe        75                8

df_Female

continent   avg_count_country   avg_age
  Asia          50                7
  Africa        60                12
  Europe        70                0

df_Transgender

continent   avg_count_country   avg_age
  Asia          30                6
  Africa        40                11
  America       80                10

Now our stacked bar grap should look like 现在,我们堆积的条形图应该看起来像

X axis will contain three ticks Male , Female , Transgender X轴将包含三个刻度线,男性,女性,变性者

Y axis will be Total_count--100 Y轴将为Total_count--100

And in the Bar avg_age will be stacked 并且在酒吧avg_age将被堆叠

Now I was trying like with pivot table 现在我正在尝试像枢轴表

pivot_df = df.pivot(index='new_Columns', columns='avg_age ', values='Values')

getting confused how to plot this , can anyone please help on how to concatenate three dataframe in one , so that it create Male,Female and Transgener columns 越来越困惑如何绘制此图,任何人都可以帮助将三个数据框合并为一个,以便它创建Male,Female和Transgener列

This topic is handeled here: https://pandas.pydata.org/pandas-docs/stable/merging.html 在此处处理该主题: https ://pandas.pydata.org/pandas-docs/stable/merging.html

(Please note, that the third continent in df_Transgender is different to the other dataframes, 'America' instead of 'Europe'; I changed that for the following plot, hoping that this is correct.) (请注意, df_Transgender中的第三大洲与其他数据df_Transgender “ America”而不是“ Europe”不同;我在下图中更改了它,希望这是正确的。)

frames = [df_Male, df_Female, df_Transgender]
df = pd.concat(frames, keys=['Male', 'Female', 'Transgender'])

              continent  avg_count_country  avg_age
Male        0      Asia                 55        5
            1    Africa                 65       10
            2    Europe                 75        8
Female      0      Asia                 50        7
            1    Africa                 60       12
            2    Europe                 70        0
Transgender 0      Asia                 30        6
            1    Africa                 40       11
            2    Europe                 80       10

btm = [0, 0, 0]
for name, grp in df.groupby('continent', sort=False):
    plt.bar(grp.index.levels[1], grp.avg_age.values, bottom=btm, tick_label=grp.index.levels[0], label=name)
    btm = grp.avg_age.values
plt.legend(ncol = 3)

在此处输入图片说明

As you commented below that America in the third dataset was no mistake, you can add rows accordingly to each dataframe like this bevor you go on like above: 正如您在下面的评论中所述,第三个数据集中的America没错,您可以像上面一样继续向每个数据框添加行,就像这样:

df_Male.append({'avg_age': 0, 'continent': 'America'}, ignore_index=True)
df_Female.append({'avg_age': 0, 'continent': 'America'}, ignore_index=True)
df_Transgender.append({'avg_age': 0, 'continent': 'Europe'}, ignore_index=True)

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

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