Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Pandas Series agg() Method

In this tutorial, we will discuss and learn the pandas Series.agg() method. This method aggregates the Series elements with one or more operations along the specified axis. It returns scalar 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 method.

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

Syntax

Series.agg(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: Aggregate the Series Elements in Pandas

Let's aggregate the Series elements using the Series.agg() method. Here, in this example, we are aggregating the elements of the Series by passing a single method to the Series.agg() 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.agg('sum'))
print("The min of the series elements is:",s.agg('min'))
print("The max of the series elements is:",s.agg('max'))
print("The mean of the series elements is:",s.agg('mean'))
print("The count of the series elements is:",s.agg('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: Aggregate the Series Elements

Let's aggregate the Series elements using the Series.agg() 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.agg() 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.agg(['sum','min','max']))


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

Example 3: Aggregate the Series Elements

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.agg(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.agg() method. We learned syntax, parameters, and solved examples by applying this method and understood the method.