Pandas aggregate method Implementation with examples

Pandas aggregate method Implementation with examples

GET FREE AMZAON AUDIOBOOKS

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 python with various examples.

Before going through the examples parts let’s know the syntax of the pandas aggregate() method.

DataFrame.aggregate(func=None, axis=0)

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)

Output

Sample Dataframe for Pandas aggregate
Sample Dataframe for Pandas aggregate

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.

df.aggregate(["sum"],axis=0)

Output

Aggregate over columns on sum
Aggregate over columns on sum

In the same, if you want to aggregate using the sum of rows then execute the below lines of code.

df.aggregate(["sum"],axis=1)

Output

Aggregate over rows on sum
Aggregate over rows on sum

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.

df.aggregate(["min"],axis=0)

Output

Aggregate over columns on min
Aggregate over columns on min

To find the minimum value along the rows then you have to pass the axis as 1.

df.aggregate(["min"],axis=1)

Output

Aggregate over rows on min
Aggregate over rows on min

 

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)

Output

Aggregate over columns on min ,sum ,max
Aggregate over columns on min,sum , max

Aggregating data on rows

df.aggregate(["sum","min","max"],axis=1)

Output

Aggregate over rows on min ,sum ,max
Aggregate over rows on min,sum, max

Conclusion

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.

Source:

Pandas Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner