Using the groupby function in a data frame

The groupby function can be used to group (and count) the number of records in a data frame that meet your criteria.

Continuing with our Titanic dataset, we can use groupby to count the number of people by age.

We can use the following script:

# read in the titanic data set
import pandas as pd
df = pd.read_excel('http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic3.xls')
# extract just the age column to its own dataset, 
# group by the age, and
# add a count for each age
ages = df[['age']].groupby('age')['age'].count()
print (ages)  

The resultant display under Jupyter is as follows. I had not realized there were so many babies on board.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.32.230