How to plot pandas groupby values in a graph?

后端 未结 1 1619
攒了一身酷
攒了一身酷 2020-12-30 12:20

I have a csv file which contains Gender and Marriage status along with few more columns like below.

Loan_ID,Gender,Married,Dependents,Education,Self_Employe         


        
相关标签:
1条回答
  • 2020-12-30 12:55

    You can use groupby + size and then use Series.plot.bar:

    Difference between count and size.

    groups = df.groupby(['Gender','Married']).size()
    groups.plot.bar()
    

    Another solution is add unstack for reshape or crosstab:

    print (df.groupby(['Gender','Married']).size().unstack(fill_value=0))
    Married   No  Yes
    Gender           
    Female    80   31
    Male     130  357
    
    df.groupby(['Gender','Married']).size().unstack(fill_value=0).plot.bar()
    

    Or:

    pd.crosstab(df['Gender'],df['Married']).plot.bar()
    

    0 讨论(0)
提交回复
热议问题