Loan_ID,Gender,MarrIEd,Dependents,Education,Self_Employed,ApplicantIncome,CoapplicantIncome,LoanAmount,Loan_Amount_Term,Credit_History,Property_Area,Loan_StatusLP001002,Male,No,Graduate,5849,360,1,Urban,YLP001003,Yes,4583,1508,128,Rural,NLP001005,3000,66,YLP001006,Not Graduate,2583,2358,120,YLP001008,6000,141,YLP001011,2,5417,4196,267,Y
我想要数不.已婚男性和女性的结果如下图所示
以下是我使用的代码:
import csvimport pandas as pdimport numpy as npimport matplotlib.pyplot as pltif __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)
分组后我有以下结果:
Gender MarrIEdFemale No 80 Yes 31Male No 130 Yes 357
我想要一个如下图表
解决方法 您可以使用groupby
size
然后使用 Series.plot.bar
: Difference between count and size.
groups = df.groupby(['Gender','MarrIEd']).size()groups.plot.bar()
另一种解决方案是为重塑或crosstab
添加unstack
:
print (df.groupby(['Gender','MarrIEd']).size().unstack(fill_value=0))MarrIEd No YesGender Female 80 31Male 130 357df.groupby(['Gender','MarrIEd']).size().unstack(fill_value=0).plot.bar()
要么:
pd.crosstab(df['Gender'],df['MarrIEd']).plot.bar()总结
以上是内存溢出为你收集整理的如何在图形中绘制pandas groupby值?全部内容,希望文章能够帮你解决如何在图形中绘制pandas groupby值?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)