Pandas is a very popular python package that allows you to manipulate large datasets of dataframe. It has many inbuilt functions for that. The pandas aggregate() method is one of them. It allows you to Aggregate data using one or more operations over the specified axis. In this entire tutorial, you will know how to implement it in pandas with various examples.
Before going through the examples parts let’s know the syntax of the pandas aggregate() method.
Here the func can be function, string, list, or dictionary. The function will be used for aggregating the data. It can be np. sum, np. mean. The axis argument can be 0 or 1. The 0 will apply the function along the column and 1 will apply the function along the rows.
The method will return a single value, Series, or Dataframe depending on the input.
Examples of Pandas Aggregate Method
Before going to the examples part first let’s create a dataframe to apply the pandas aggregate method. You can create dataframe using the pandas. DataFrame method. Let’s create it.
import pandas as pd import numpy as np array = np.array([[10,20,30],[40,50,60],[70,80,90]]) df = pd.DataFrame(array,columns=["col1","col2","col3"]) print(df)
Example 1: Aggregate data using the sum
If you want to aggregate data using sum on each column then you have to pass the “sum” and axis =0 as the argument inside the aggregate() method.
Execute the below lines of code.
In the same, if you want to aggregate using the sum of rows then execute the below lines of code.
Example 2: Aggregate data using the minimum value
Now if you want to aggregate data using the minimum value then you have to pass the min function. To find the minimum value along with columns then you have to use the axis = 0. Use the below line of code to do so.
To find the minimum value along the rows then you have to pass the axis as 1.
Example 3: Aggregate data using more than one function
In the above examples, you have to use separate functions min, max, and sum in the separate lines of code. In this example, you will use both in single lines of code.
Aggregating data on columns
df.aggregate(["sum","min","max"],axis = 0)
Aggregating data on rows
These are examples of how to use the pandas aggregate method on your dataframe. You can use this function to group data or finding specific categories. I hope you have got the answers to your queries. If you have any doubt then you can contact us for more help.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.