简体   繁体   English

Python 多个数据框到一张图中

[英]Python Multiple dataframes into one graph

So, I was able to create 3 data frames out of my original data, posted below.因此,我能够根据下面发布的原始数据创建 3 个数据框。 The other is a female data frame but it's "female" under the "sex" column and the "charges" values are different.另一个是女性数据框,但它在“性别”列下是“女性”,“费用”值不同。

DF1
children    charges
    0   12365.975602
    1   12731.171832
    2   15073.563734
    3   15355.318367
    4   13850.656311
    5   8786.035247

MALE DF (Female DF like this but with female under sex and different charge values)
children sex    charges
    0    male   12832.696736
    1    male   13273.522458
    2    male   16187.095325
    3    male   16789.167419
    4    male   13782.284829
    5    male   7931.658310

What I was wanting to do is combine all 3 data frames into one bar graph.我想做的是将所有 3 个数据框组合成一个条形图。 A bar for female, a bar for male, and a bar for both genders combined (DF1).一个女性酒吧,一个男性酒吧,以及一个男女结合的酒吧(DF1)。 How would I do that?我该怎么做? Thanks!谢谢!

I assume that you want to have a Barchart of the number of males, females and the sum of them.我假设您想要一个包含男性、女性数量及其总和的条形图。 This is an example with plolty.这是 plotty 的一个例子。 In matplotlib it is much similar.在 matplotlib 中,它非常相似。

male = pd.DataFrame([[1,2,3], [1,2,3]])
female = pd.DataFrame([[2,2,3], [2,2,3], [1,2,1]])
both = pd.DataFrame([[1,2,3], [1,2,3]])
go.Figure(
    data=[
        go.Bar(
            name='Male',
            x=['Male', 'Female', 'Both'],
            y=[male.shape[0], female.shape[0], female.shape[0]+male.shape[0]] 
        ),
        
    ]
).show()

https://plotly.com/python/bar-charts/ https://plotly.com/python/bar-charts/

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

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