Agg python – Python Pandas Series agg() Function

Pandas Series agg() Function:

Agg python: The agg() method of the  Pandas Series is used to do aggregation using one or more operations over the given axis.

Syntax:

Series.agg(func=None, axis=0)

Parameters

func: This is required. It Indicates the function that will be used to aggregate the data. If a function, should either work when passed a Series or when passed to Series.apply. accepted combinations are as follows.

  • function
  • string function name
  • list of functions and/or function names, e.g. [np.sum, ‘mean’]
  • dictionary of axis labels -> functions, function names, or list of such.

axis: This is optional. This argument is required for DataFrame compatibility. It indicates a value of 0 or ‘index’. This is an axis on which the function is applied.

Return Value:

The following is returned:

  • When calling Series.agg with a single function, the result is a Scalar.
  • When DataFrame.agg is called with a single function, it returns a Series.
  • When DataFrame.agg is called with multiple functions, it returns a DataFrame.

Pandas Series agg() Function in Python:

Example1

Approach:

  • Import pandas module using the import keyword.
  • Import numpy module using the import keyword.
  • Pass some random list as an argument to the Series() function of the pandas module to create a series.
  • Store it in a variable.
  • Print the above-given series.
  • Apply numpy.sum, agg() function on the given series to add all the elements of the given series and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Import numpy module using the import keyword.
import numpy as np
# Pass some random list as an argument to the Series() function
# of the pandas module to create a series.
# Store it in a variable.
gvn_series = pd.Series([10, 5, 2, 4])
# Print the above given series
print("The given series is:")
print(gvn_series)
print()
# Apply numpy.sum, agg() function on the given series to add 
# all the elements of the given series and print the result.
print("Adding all the elements of the given series:")
print(gvn_series.agg(np.sum))

Output:

The given series is:
0    10
1     5
2     2
3     4
dtype: int64

Adding all the elements of the given series:
21

Example2: Performing multiple operations on a Series

Python agg: Here sum, variance, mean and average operations are performed on a given series.

Approach:

  • Import pandas module using the import keyword.
  • Import numpy module using the import keyword.
  • Pass some random list as an argument to the Series() function of the pandas module to create a series.
  • Store it in a variable.
  • Print the above-given series.
  • Pass list of operations(sum, variance, mean, average) to the agg() function to do multiple operations on the given series and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Import numpy module using the import keyword.
import numpy as np
# Pass some random list as an argument to the Series() function
# of the pandas module to create a series.
# Store it in a variable.
gvn_series = pd.Series([10, 5, 2, 4])
# Print the above given series
print("The given series is:")
print(gvn_series)
print()
# Pass list of operations(sum, variance, mean, average) to the agg() function 
# to do multiple operations on the given series and print the result.
print("Performing sum, variance, mean, average operations on the given series:")
print(gvn_series.agg([np.sum, np.var, np.mean, 'average']))

Output:

The given series is:
0    10
1     5
2     2
3     4
dtype: int64

Performing sum, variance, mean, average operations on the given series:
sum        21.000000
var        11.583333
mean        5.250000
average     5.250000
dtype: float64

Example3

Approach:

  • Import pandas module using the import keyword.
  • Import numpy module using the import keyword.
  • Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
  • Store it in a variable.
  • Print the given dataframe.
  • Apply agg() function on the student_marks column of the dataframe to perform the addition operation of all the values of the student_marks column and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Import numpy module using the import keyword.
import numpy as np
# Pass some random key-value pair(dictionary), index list as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "student_rollno": [1, 2, 3, 4],
  "student_marks": [80, 35, 25, 90]},
  index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()
# Apply agg() function on the student_marks column of the dataframe to
# perform the addition operation of all the values of the student_marks
# column and print the result.
print("The addition of all the values of the student_marks column of the dataframe:")
print(data_frme['student_marks'].agg(np.sum))

Output:

The given Dataframe:
        student_rollno  student_marks
virat                1             80
nick                 2             35
jessy                3             25
sindhu               4             90

The addition of all the values of the student_marks column of the dataframe:
230