Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator
pandas

How to apply multiple aggregations in pandas

Harsh Jain

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 pd

drinks = pd.read_csv('http://bit.ly/drinksbycountry')

beer_mean = drinks.groupby('continent').beer_servings.mean()
print(beer_mean)
Single aggregation on a DataFrame in Pandas

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 column continent and then apply the aggregation on the beer_savings column.

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 pd

drinks = 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)
Multiple aggregation on a DataFrame and Series object in Pandas

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, and max, and are all grouped by continent.
  • In line 9, we apply the agg() function on a Series object using the column beer_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.

RELATED TAGS

python
communitycreator
pandas
RELATED COURSES

View all Courses

Keep Exploring