How to apply multiple aggregations in pandas
In this shot, we will learn how to apply different aggregation functions on a DataFrame and Series object in pandas.
Single aggregation on a DataFrame
There are different aggregation functions (like mean(), min(), max(), etc.) that you can apply on a DataFrame or Series object. In the code snippet below, we have applied the mean() aggregation after applying a groupby() function on the DataFrame.
import pandas as pddrinks = pd.read_csv('http://bit.ly/drinksbycountry')beer_mean = drinks.groupby('continent').beer_servings.mean()print(beer_mean)
Explanation:
- In line 1, we import the required package.
- In line 3, we read the CSV file from the URL.
- In line 5, we apply
groupby()on the columncontinentand then apply the aggregation on thebeer_savingscolumn.
Multiple aggregations on a DataFrame and Series object
Now, let’s see how we can apply multiple aggregation functions on a DataFrame object as well as a Series object.
Take a look at the code snippet below:
import pandas as pddrinks = pd.read_csv('http://bit.ly/drinksbycountry')beer_mean = drinks.groupby('continent').beer_servings.agg(['mean', 'min', 'max'])print(beer_mean)aggregation_series = drinks.beer_servings.agg(['mean', 'min', 'max'])print(aggregation_series)aggregation_df = drinks.agg(['mean', 'min', 'max'])print(aggregation_df)
Explanation:
- In line 1, we import the required package.
- In line 3, we read the CSV file from the URL.
- In line 5, we use the
agg()function and then pass in all the aggregations that we want to apply after grouping them. - In line 7, we print the aggregation results. Here, we can see that the results have a
mean,min, andmax, and are all grouped bycontinent. - In line 9, we apply the
agg()function on a Series object using the columnbeer_savings. - In line 12, we apply the
agg()function on the complete DataFrame and then print the results.
You can easily apply multiple aggregation functions with one line of code on a DataFrame and Series object in pandas.