To ensure you can follow along, we are using pandas 2.2.0, which is the latest version available at the time of writing this article.

You are probably already familiar with performing aggregations in pandas using methods such as sum or min. You have also probably used these methods in combination with groupby. Therefore, it will not come as a surprise that the agg method is used to perform one or more aggregations on a DataFrame. What is interesting is that we can use agg in a few ways, depending on the syntax we use. Let’s illustrate this with some examples.

By passing a dictionary to the agg method, we indicate which aggregations (sum, mean, max, etc.) we want to calculate for each column of the DataFrame. The keys of the dictionary represent the columns on which we want to perform the aggregations, while the values represent the operations we want to execute.



Source link