+

Python | Pandas Series.aggregate ()

Series.aggregate() Pandas Series.aggregate() aggregates using one or more operations on the specified axis in this 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 () to perform an aggregation of the underlying data of a given series object.

# import pandas as pd

< p> import pandas as pd

 
# Create series

sr = pd.Series ([ 34 , 5 , 13 , 32 , 4 , 15 ])

 
# Create index

index_ = [ `Coca Cola` , ` Sprite ` , ` Coke` , `Fanta` , ` Dew ` , ` ThumbsUp` ]

 
# set index

sr.index = index_

 
# Print series

print (sr)

Output:

 Coca Cola 34 Sprite 5 Coke 13 Fanta 32 Dew 4 ThumbsUp 15 dtype: int64 

We will now use Series.aggregate () to find the sum of all values ​​in this series object.

# Find the sum of all values ​​

result = sr.aggregate (func = sum )

 
# Print result

print (result)

Output:

 103 

As we can see in the output, Series.aggregate () successfully returned the sum of the underlying data for this series object.

Example # 2: Use Series.aggregate () to aggregate the underlying data for this series object.

# import pandas as pd

import < / code> pandas as pd

 
# Create a series

sr = pd. Series ([ 51 , 10 , 24 , 18 , 1 , 84 , 12 , 10 , 5 , 24 , 0 ])

 
# Create index
# apply annual rate

index_ = pd.date_range ( `2010-10-09 08:45` , periods = 11 , freq = `Y` )

  
# set index

sr.index = index_

 
# Print series

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 

We will now use Series. aggregate () to find the maximum of all values ​​in a given series object.

# Find the maximum of all values ​​

result = sr.aggregate (func = max )

  
# Print result

print (result)

Output:

84

As we can see in the output, Seri es.aggregate () successfully returned the maximum of all values ​​in this series object.

Get Solution for free from DataCamp guru