How to create a histogram from grouped data

I am trying to create a histogram from grouped data in pandas.

So far, I have managed to create a standard plot line. But I can’t understand how to do the same to get a histogram (histogram). I would like to get 2 age histograms of people who have survived in the Titanic and who haven’t - to see if there is a difference in the distribution of age.

Source data: https://www.udacity.com/api/nodes/5454512672/supplemental_media/titanic-datacsv/download

So far my code is:

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 try to build a histogram from this dataset, I get strange results.

SurvivedAget.hist()

I would be grateful for your help in this.

+4
1

:

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')

:

enter image description here

. , , bin , , 5yr , seaborn .

+3

Source: https://habr.com/ru/post/1624975/


All Articles