简体   繁体   中英

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

I have three dataframes df_Male , df_female , Df_TransGender

sample dataframe 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

Y axis will be Total_count--100

And in the Bar avg_age will be stacked

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

This topic is handeled here: 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.)

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:

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)

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