如何从分组数据创建直方图 [英] How to create histogram from grouped data

查看:467
本文介绍了如何从分组数据创建直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据熊猫中的分组数据创建直方图.

I'm trying to create histogram from grouped data in pandas.

到目前为止,我已经能够创建标准折线图.但是我不知道如何做以获得直方图(条形图).我想获得2个在泰坦尼克号暗恋中幸存下来但没有幸存的人的年龄直方图-看看年龄分布是否存在差异.

So far I was able to create standard line plot. But I can't figure out how to do the same to get histogram (bar chart). I would like to get 2 age histograms of persons who survived Titanic crush and who didn't - to see if there is a difference in age distribution.

源数据: https://www.udacity.com/api/nodes/5454512672 /supplemental_media/titanic-datacsv/download

到目前为止,我的代码:

So far my code:

import pandas as pn
titanic = pn.DataFrame.from_csv('titanic_data.csv')

SurvivedAge= titanic.groupby(['Survived','Age']).size()
SurvivedAge=SurvivedAge.reset_index()
SurvivedAge.columns=['Survived', 'Age', 'Num']
SurvivedAge.index=(SurvivedAge['Survived'])
del SurvivedAge['Survived']

SurvivedAget=SurvivedAge.reset_index().pivot('Age', 'Survived','Num')

SurvivedAget.plot() 

当我试图从该数据集中绘制直方图时,我得到的结果很奇怪.

when I'm trying to plot a histogram from this data set I'm getting strange results.

SurvivedAget.hist()

感谢您的帮助.

推荐答案

您可以:

titanic = pd.read_csv('titanic_data.csv')
survival_by_age = titanic.groupby(['Age', 'Survived']).size().unstack('Survived')
survival_by_age.columns = ['No', 'Yes']
survival_by_age.plot.bar(title='Survival by Age')

获得:

,您可以进一步调整 .您还可以合并小数年龄,以便使用整数索引,或者使用 seaborn 和一个各种分布图.

which you can further tweak. You could also consolidate the fractional ages so you can use integer indices, or bin the data into say 5yr age spans to get more user-friendly output. And then there is seaborn with a various types of distribution plots.

这篇关于如何从分组数据创建直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆