Pandas Series agg() Method
Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Series.agg() is used to pass a function or list of functions to be applied on a series or even each element of the series separately. In the case of a list of functions, multiple results are returned by the Series.agg() method.
Pandas Series Aggregate Syntax
Syntax: Series.agg(func, axis=0)
Parameters:
- func: Function, list of function or string of function name to be called on Series.
- axis:0 or ‘index’ for row wise operation and 1 or ‘columns’ for column wise operation.
Return Type: The return type depends on return type of function passed as parameter.
Aggregate the Series Elements in Pandas
In Pandas, series elements can be aggregated by computing statistical measures such as sum, mean, min, max, and count. These functions can provide insights into the dataset’s characteristics.
Python3
import pandas as pd s = pd.Series([ 89 , 99 , 78 , 70 ]) s.agg( 'min' ) s.agg([ 'min' , 'max' ]) |
Output:
min 70
max 99
Example 1: In this example, a Python lambda function is passed which simply adds 2 to each value of the series. Since the function will be applied to each value of the series, the return type is also a series. A random series of 10 elements is generated by passing an array generated using Numpy random method.
Python3
# importing pandas module import pandas as pd # importing numpy module import numpy as np # creating random arr of 10 elements arr = np.random.randn( 10 ) # creating series from array series = pd.Series(arr) # calling .agg() method result = series.agg( lambda num: num + 2 ) # display print ( 'Array before operation: \n' , series, '\n\nArray after operation: \n' , result) |
Output:
As shown in the output, the function was applied to each value and 2 was added to each value of the series.
Array before operation:
0 -0.178400
1 -0.014408
2 -2.185778
3 0.335517
4 1.013446
5 0.897206
6 0.116324
7 -1.046006
8 -0.918818
9 0.552542
dtype: float64
Array after operation:
0 1.821600
1 1.985592
2 -0.185778
3 2.335517
4 3.013446
5 2.897206
6 2.116324
7 0.953994
8 1.081182
9 2.552542
dtype: float64
Example 2: Passing List of functions In this example, a list of some of Python’s default functions is passed and multiple results are returned by Pandas Series.agg() method into multiple variables.
Python3
# importing pandas module import pandas as pd # importing numpy module import numpy as np # creating random arr of 10 elements arr = np.random.randn( 10 ) # creating series from array series = pd.Series(arr) # creating list of function names func_list = [ min , max , sorted ] # calling .agg() method # passing list of functions result1, result2, result3 = series.agg(func_list) # display print ( 'Series before operation: \n' , series) print ('\nMin = {}\n\nMax = {},\ \n\nSorted Series:\n{}'. format (result1, result2, result3)) |
Output:
As shown in the output, multiple results were returned. Min, Max, and Sorted array were returned into different variables result1, result2, and result3 respectively.
Series before operation:
0 -1.493851
1 -0.658618
2 0.265253
3 -0.503875
4 1.419182
5 0.221025
6 -0.712019
7 -1.462868
8 -0.341504
9 -0.338337
dtype: float64
Min = -1.4938513079840412
Max = 1.4191824761086351,
Sorted Series:
[-1.4938513079840412, -1.462868259420631, -0.7120191767162937, -0.6586184541010776,
-0.5038754446324809, -0.34150351227216663, -0.33833663286234356,
0.22102480822109685, 0.2652526809574672, 1.4191824761086351]