In this shot, we will learn how to apply different aggregation functions on a DataFrame and Series object in pandas.
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)
groupby() on the column continent and then apply the aggregation on the beer_savings column.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)
agg() function and then pass in all the aggregations that we want to apply after grouping them.mean, min, and max, and are all grouped by continent.agg() function on a Series object using the column beer_savings.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.