简体   繁体   中英

Python Multiple dataframes into one graph

So, I was able to create 3 data frames out of my original data, posted below. 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. A bar for female, a bar for male, and a bar for both genders combined (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. In matplotlib it is much similar.

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/

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