In [2]: titanic=pd.read_csv("data/titanic.csv")In [3]: titanic.head()Out[3]: PassengerId Survived Pclass ... Fare Cabin Embarked0 1 0 3 ... 7.2500 NaN S1 2 1 1 ... 71.2833 C85 C2 3 1 3 ... 7.9250 NaN S3 4 1 1 ... 53.1000 C123 S4 5 0 3 ... 8.0500 NaN S[5 rows x 12 columns]
Aggregating statistics
What is the average age of the Titanic passengers?
In [4]: titanic["Age"].mean()Out[4]: 29.69911764705882
Different statistics are available and can be applied to columns with
numerical data. Operations in general exclude missing data and operate
across rows by default.
What is the median age and ticket fare price of the Titanic passengers?
In [5]: titanic[["Age","Fare"]].median()Out[5]: Age 28.0000Fare 14.4542dtype: float64
The statistic applied to multiple columns of a DataFrame (the selection of two columns
returns a DataFrame, see the subset data tutorial) is calculated for each numeric column.
The aggregating statistic can be calculated for multiple columns at the
same time. Remember the describe function from the first tutorial?
Instead of the predefined statistics, specific combinations of
aggregating statistics for given columns can be defined using the
DataFrame.agg() method:
Details about descriptive statistics are provided in the user guide section on descriptive statistics.
Aggregating statistics grouped by category
What is the average age for male versus female Titanic passengers?
In [8]: titanic[["Sex","Age"]].groupby("Sex").mean()Out[8]: AgeSex female 27.915709male 30.726645
As our interest is the average age for each gender, a subselection on
these two columns is made first: . Next, the
method is applied on the column to make a group per
category. The average age is calculated and
returned.
Count number of records by category
What is the number of passengers in each of the cabin classes?
In [12]: titanic["Pclass"].value_counts()Out[12]: Pclass3 4911 2162 184Name: count, dtype: int64
The value_counts() method counts the number of records for each
category in a column.
The function is a shortcut, as it is actually a groupby operation in combination with counting of the number of records
within each group:
Calculating a given statistic (e.g. mean age) for each category in
a column (e.g. male/female in the Sex column) is a common pattern.
The groupby method is used to support this type of operations. This
fits in the more general split-apply-combine pattern:
Split the data into groups
Apply a function to each group independently
Combine the results into a data structure
The apply and combine steps are typically done together in pandas.
In the previous example, we explicitly selected the 2 columns first. If
not, the mean method is applied to each column containing numerical
columns by passing numeric_only=True:
It does not make much sense to get the average value of the Pclass.
If we are only interested in the average age for each gender, the
selection of columns (rectangular brackets [] as usual) is supported
on the grouped data as well:
In [10]: titanic.groupby("Sex")["Age"].mean()Out[10]: Sexfemale 27.915709male 30.726645Name: Age, dtype: float64
What is the mean ticket fare price for each of the sex and cabin class combinations?
In [11]: titanic.groupby(["Sex","Pclass"])["Fare"].mean()Out[11]: Sex Pclassfemale 1 106.125798 2 21.970121 3 16.118810male 1 67.226127 2 19.741782 3 12.661633Name: Fare, dtype: float64
Grouping can be done by multiple columns at the same time. Provide the
column names as a list to the groupby() method.
To user guide
A full description on the split-apply-combine approach is provided in the user guide section on groupby operations.
In [13]: titanic.groupby("Pclass")["Pclass"].count()Out[13]: Pclass1 2162 1843 491Name: Pclass, dtype: int64
To user guide
The user guide has a dedicated section on value_counts , see the page on discretization.
REMEMBER
Aggregation statistics can be calculated on entire columns or rows.
groupby provides the power of the split-apply-combine pattern.
value_counts is a convenient shortcut to count the number of
entries in each category of a variable.
To user guide
A full description on the split-apply-combine approach is provided in the user guide pages about groupby operations.