pandas 如何在图中绘制熊猫 groupby 值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45299305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to plot pandas groupby values in a graph?
提问by pythonaddict
I have a csv file which contains Gender and Marriage status along with few more columns like below.
我有一个 csv 文件,其中包含性别和婚姻状况以及如下所示的更多列。
Loan_ID,Gender,Married,Dependents,Education,Self_Employed,ApplicantIncome,CoapplicantIncome,LoanAmount,Loan_Amount_Term,Credit_History,Property_Area,Loan_Status
LP001002,Male,No,0,Graduate,No,5849,0,,360,1,Urban,Y
LP001003,Male,Yes,1,Graduate,No,4583,1508,128,360,1,Rural,N
LP001005,Male,Yes,0,Graduate,Yes,3000,0,66,360,1,Urban,Y
LP001006,Male,Yes,0,Not Graduate,No,2583,2358,120,360,1,Urban,Y
LP001008,Male,No,0,Graduate,No,6000,0,141,360,1,Urban,Y
LP001011,Male,Yes,2,Graduate,Yes,5417,4196,267,360,1,Urban,Y
I want to count no. of married Males and Females and show the same in graph as shown below
我想数不。已婚男性和女性,并在图表中显示相同,如下所示
Below is the code I am using :
以下是我正在使用的代码:
import csv
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
if __name__ == '__main__':
x=[]
y=[]
df = pd.read_csv(
"/home/train.csv",usecols=[1,2]).dropna(subset=['Gender','Married']) # Reading the dataset in a dataframe using Pandas
groups = df.groupby(['Gender','Married'])['Married'].apply(lambda x: x.count())
print(groups)
After group by I have following result :
分组后我有以下结果:
Gender Married
Female No 80
Yes 31
Male No 130
Yes 357
I want a chart like below
我想要一个像下面这样的图表
回答by jezrael
You can use groupby+ sizeand then use Series.plot.bar:
您可以使用groupby+size然后使用Series.plot.bar:
Difference between count and size.
groups = df.groupby(['Gender','Married']).size()
groups.plot.bar()
Another solution is add unstackfor 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()


