Pandas Series agg() Method
Last Updated :
23 Aug, 2023
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
import pandas as pd
import numpy as np
arr = np.random.randn( 10 )
series = pd.Series(arr)
result = series.agg( lambda num: num + 2 )
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
import pandas as pd
import numpy as np
arr = np.random.randn( 10 )
series = pd.Series(arr)
func_list = [ min , max , sorted ]
result1, result2, result3 = series.agg(func_list)
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]
Similar Reads
Pandas Series.fillna() Method
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series fillna() Syntax Pandas Series
3 min read
Creating a Pandas Series
A Pandas Series is like a single column of data in a spreadsheet. It is a one-dimensional array that can hold many types of data such as numbers, words or even other Python objects. Each value in a Series is associated with an index, which makes data retrieval and manipulation easy. This article exp
3 min read
Python | Pandas Series.get()
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.get() function get item from
2 min read
Python | Pandas Series.mod()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Python Series.mod() is used to return the remainder after division of two numbers Synt
3 min read
Python | Pandas Series.aggregate()
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.aggregate() function aggregat
3 min read
Python | Pandas Series.str.isspace() method
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analysing data much easier. Pandas isspace() is a string method, it checks for All-Space characters in a series an
3 min read
Accessing elements of a Pandas Series
Pandas Series is a one-dimensional labeled array capable of holding data of any type (integer, string, float, python objects, etc.). Labels need not be unique but must be a hashable type. An element in the series can be accessed similarly to that in an ndarray. Elements of a series can be accessed i
6 min read
Python | Pandas Series.mode()
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.mode() function return the mo
2 min read
Python | Pandas Series.ge()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas series.ge() is used to compare every element of Caller series with passed serie
3 min read
Python | Pandas Series.at
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Series.at attribute enables us to access a single value for a row/column label
2 min read