Python | Pandas Series.aggregate()
Last Updated :
27 Feb, 2019
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 aggregate using one or more operations over the specified axis in the given series object.
Syntax: Series.aggregate(func, axis=0, *args, **kwargs)
Parameter :
func : Function to use for aggregating the data.
axis : Parameter needed for compatibility with DataFrame.
*args : Positional arguments to pass to func.
**kwargs : Keyword arguments to pass to func.
Returns : DataFrame, Series or scalar
Example #1: Use Series.aggregate()
function to perform aggregation on the underlying data of the given series object.
import pandas as pd
sr = pd.Series([ 34 , 5 , 13 , 32 , 4 , 15 ])
index_ = [ 'Coca Cola' , 'Sprite' , 'Coke' , 'Fanta' , 'Dew' , 'ThumbsUp' ]
sr.index = index_
print (sr)
|
Output :
Coca Cola 34
Sprite 5
Coke 13
Fanta 32
Dew 4
ThumbsUp 15
dtype: int64
Now we will use Series.aggregate()
function to find the sum of all the values in the given series object.
result = sr.aggregate(func = sum )
print (result)
|
Output :
103
As we can see in the output, the Series.aggregate()
function has successfully returned the sum of the underlying data of the given series object.
Example #2 : Use Series.aggregate()
function to perform aggregation on the underlying data of the given series object.
import pandas as pd
sr = pd.Series([ 51 , 10 , 24 , 18 , 1 , 84 , 12 , 10 , 5 , 24 , 0 ])
index_ = pd.date_range( '2010-10-09 08:45' , periods = 11 , freq = 'Y' )
sr.index = index_
print (sr)
|
Output :
2010-12-31 08:45:00 51
2011-12-31 08:45:00 10
2012-12-31 08:45:00 24
2013-12-31 08:45:00 18
2014-12-31 08:45:00 1
2015-12-31 08:45:00 84
2016-12-31 08:45:00 12
2017-12-31 08:45:00 10
2018-12-31 08:45:00 5
2019-12-31 08:45:00 24
2020-12-31 08:45:00 0
Freq: A-DEC, dtype: int64
Now we will use Series.aggregate()
function to find the maximum of all the values in the given series object.
result = sr.aggregate(func = max )
print (result)
|
Output :
84
As we can see in the output, the Series.aggregate()
function has successfully returned the maximum of all the values in the given series object.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...