Signup/Sign In
PUBLISHED ON: MARCH 31, 2021

Pandas Series aggregate() Method

In this tutorial, we will discuss and learn the Pandas Series.aggregate() method. This method aggregates the Series elements with one or more operations along the specified axis. It returns a scalar value when the method is called by a single method and it returns multiple outputs when the method is called by a multiple or list of methods.

The below is the syntax of the Series.aggregate() method.

Syntax

Series.aggregate(func=None, axis=0, *args, **kwargs)

Parameters

func: It represents the method, str, list, or dict. It is the method used to aggregate the data.

axis: If 0 or 'index' that applies a method to each column. If 1 or 'columns', that apply a method to each row. Default axis value is 0 or 'index'.

*args: It represents the positional arguments to pass to func.

**kwargs: It represents the keyword arguments to pass to func.

Example: Pandas Series.aggregate() Method

Let's aggregate the Series elements using the Series.aggregate() method. Here, in this example, we are aggregating the elements of the Series by passing a single method to the Series.aggregate() method. See the below example.

We aggregated using the 'sum', 'min', 'max', 'mean', 'count' methods.

#importing pandas as pd
import pandas as pd
#creating Series
s= pd.Series([2,3,4])
print("------After aggregating the result is------")
print("The sum of the series elements is:",s.aggregate('sum'))
print("The min of the series elements is:",s.aggregate('min'))
print("The max of the series elements is:",s.aggregate('max'))
print("The mean of the series elements is:",s.aggregate('mean'))
print("The count of the series elements is:",s.aggregate('count'))


------After aggregating the result is------
The sum of the series elements is: 9
The min of the series elements is: 2
The max of the series elements is: 4
The mean of the series elements is: 3.0
The count of the series elements is: 3

Example 2: Pandas Series.aggregate() Method

Let's aggregate the Series elements using the Series.aggregate() method. Here, in this example, we are aggregating the elements of the Series by passing list of methods to the Series.agg() method.

We passed the 'sum', 'min', 'max' methods to the list and the Series.aggregate() method returns the multiple outputs. See the below example.

#importing pandas as pd
import pandas as pd
#creating Series
s= pd.Series([2,3,4])
print("The ouput of the agg method is:\n",s.aggregate(['sum','min','max']))


The ouput of the agg method is:
sum 9
min 2
max 4
dtype: int64

Example 3: Pandas Series.aggregate() Method

We can aggregate the Series elements using the user-defined methods. Create a user-defined method, in this example, we created add() method which adds value 1 to the element of Series, if the element is greater than 3 otherwise it returns the same element. See the below example.

#importing pandas as pd
import pandas as pd
#defining a user-defined function
def add(x):
    if x>3:
        return x+1
    else:
        return x
#creating Series
s= pd.Series([3,7,5,2,9,4,2])
print("------After aggregating the result is------")
print(s.aggregate(add))


------After aggregating the result is------
0 3
1 8
2 6
3 2
4 10
5 5
6 2
dtype: int64

Conclusion

In this tutorial, we learned the Python pandas DataFrame.aggregate() method. We learned syntax, parameters, and solved examples by applying this method to series.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.