【发布时间】:2020-06-18 16:40:15
【问题描述】:
如何从下面列出的数据框中绘制直方图?
我想根据Education 列可视化每个教育级别的女性人数。
从下面打印我们的输出示例:
高中 30 岁的女性
大学 33 中的女性
单身女性 14
我尝试了什么
#show max rows and columns
pd.set_option('display.max_rows', 1000)
countFemales = myDataFrame['Gender'].str.contains("Female").sum()
#subset myDataFrame based on Gender's value, returns boolean series
isFemale = myDataFrame['Gender']=='Female'
#fileter dataframe based on boolean condition, extract female column as df
femaleDataframe = myDataFrame[isFemale]
# extract only unique values from female data: Bachelor, Colleage, High Scool..
femaleLevelOfEducation = femaleDataframe.Education.unique()
print("women in High Scool " + str(femaleDataframe["Education"].str.contains("High School or Below").sum()))
print("women in College " + str(femaleDataframe["Education"].str.contains("College").sum()))
print("women in Bachelor " + str(femaleDataframe["Education"].str.contains("Bachelor").sum()))
femaleDataframe.plot(x=femalLevelOfEducation, y=countFemales, kind='hist')
plt.show() //this is where I am stuck
编辑
如果我使用plt.bar(x=femaleLevelOfEducation, y=countFemales, height=60),我会得到如下所示的条形图。但是,这对我来说没有意义,因为根据打印语句,在数据集中,有:
高中 30 岁的女性
大学 33 中的女性
单身女性 14
那么现在的问题是,为什么 y 轴伸展到 140 而不是最大 33?
数据集:https://drive.google.com/file/d/1Y8VdU1Y7jGR17vWDspm31PdL-d1BQlDg/view?usp=sharing
【问题讨论】:
-
我认为如果您以可用格式而不是屏幕截图提供数据,您将获得更好的帮助(提示,请参阅:stackoverflow.com/help/minimal-reproducible-example)
-
我会尽快做的
标签: python-3.x pandas dataframe