Signup/Sign In

Pandas DataFrame agg() Method

In this tutorial, we will learn the python pandas DataFrame.agg() method. This method aggregates using one or more operations over the specified axis i.e rows or columns.

It returns a scalar, Series, or DataFrame according to the function. It returns Series when DataFrame.agg is called with a single function and DataFrame when DataFrame.agg is called with several functions.

The below shows the syntax of this function in python pandas.

Syntax

The syntax required to use this function is as follows:

DataFrame.aggregate(func=None, axis=0, *args, **kwargs)

Parameters

Let us take a look at the parameters of this function:

func: It is used to aggregate the data. It may be a function, string function name, list of functions, or function name. For example: [np.sum, 'min']

axis: If 0 or 'index' that applies a function to each column. If 1 or 'columns', that apply a function to each row. Default axis value is 0 or 'index'.

*args: Positional arguments to pass to func.

**kwargs: Keyword arguments to pass to func.

Example: Aggregating DataFrame with a single function over the rows

The below example shows how to apply DataFrame.agg() method with a single function. The aggregation operations are always performed over an axis.

import pandas as pd
df = pd.DataFrame([[1, 2, 3],[4, 5, 6],[7,8,9],[10,11,12]],columns=['A', 'B','C'])
print("Printing number of non-null values in DataFrame")
print(df.agg(["count"]))
print("Printing the sum of values in DataFrame")
print(df.agg(["sum"]))

Once we run the program we will get the following result.


Printing number of non-null values in DataFrame
A B C
count 4 4 4
Printing the sum of values in DataFrame
A B C
sum 22 26 30

Example: Aggregating DataFrame with a single function over the rows

The below shows another example of how to apply DataFrame.agg() method with a single function.

import pandas as pd
df = pd.DataFrame([[1, 2, 3],[4, 5, 6],[7,8,9],[10,11,12]],columns=['A', 'B','C'])
print("Printing the minimum value in DataFrame")
print(df.agg(["min"]))
print("Printing the maximum value in DataFrame")
print(df.agg(["max"]))

Once we run the program we will get the following result.


Printing the minimum value in DataFrame
A B C
min 1 2 3
Printing the maximum value in DataFrame
A B C
max 10 11 12

Example: Aggregating DataFrame with a single function over the columns

The below example shows how to apply DataFrame.agg() method with a single function. The aggregation operations are always performed over an axis, here the axis=1 or 'columns'.

import pandas as pd
df = pd.DataFrame([[1, 2, 3],[4, 5, 6],[7,8,9],[10,11,12]],columns=['A', 'B','C'])
print("Printing number of non-null values in DataFrame")
print(df.agg(["count"],axis='columns'))
print("Printing the sum of values in DataFrame")
print(df.agg(["sum"],axis='columns'))

Once we run the program we will get the following result.


Printing number of non-null values in DataFrame
count
0 3
1 3
2 3
3 3
Printing the sum of values in DataFrame
sum
0 6
1 15
2 24
3 33

Example: Aggregating DataFrame with a single function over the columns

The below shows another example of how to apply DataFrame.agg() method with a single function.

import pandas as pd
df = pd.DataFrame([[1, 2, 3],[4, 5, 6],[7,8,9],[10,11,12]],columns=['A', 'B','C'])
print("Printing the minimum value in DataFrame")
print(df.agg(["min"],axis='columns'))
print("Printing the maximum value in DataFrame")
print(df.agg(["max"],axis='columns'))

Once we run the program we will get the following result.


Printing the minimum value in DataFrame
min
0 1
1 4
2 7
3 10
Printing the maximum value in DataFrame
max
0 3
1 6
2 9
3 12

Example: Another example of Aggregating DataFrame with a single function over the columns.

The below shows another example of how to apply DataFrame.agg() method with a single function.

import pandas as pd
df = pd.DataFrame([[1, 2, 3],[4, 5, 6],[7,8,9],[10,11,12]],columns=['A', 'B','C'])
print("Printing the mean of values in DataFrame")
print(df.agg(["mean"],axis='columns'))
print("Printing the median of values in DataFrame")
print(df.agg(["median"],axis='columns'))

Once we run the program we will get the following result.


Printing the mean of values in DataFrame
mean
0 2.0
1 5.0
2 8.0
3 11.0
Printing the median of values in DataFrame
median
0 2.0
1 5.0
2 8.0
3 11.0

Example: Aggregating DataFrame with the list of functions over the rows and columns.

The below example shows how to apply DataFrame.agg() method with the list of functions.

import pandas as pd
df = pd.DataFrame([[1, 2, 3],[4, 5, 6],[7,8,9],[10,11,12]],columns=['A', 'B','C'])
print("printing sum and min of the dataframe with default axis")
print(df.agg(["sum","min"]))
print("printing sum and min of the dataframe with the axis is equal to columns")
print(df.agg(["sum","min"],axis='columns'))

Once we run the program we will get the following result.


printing sum and min of the dataframe with default axis
A B C
sum 22 26 30
min 1 2 3
printing sum and min of the dataframe with the axis is equal to columns
sum min
0 6 1
1 15 4
2 24 7
3 33 10

Example: Aggregating different functions over the columns of DataFrame.

The below example shows how to apply DataFrame.agg() method with the different functions.

import pandas as pd
df = pd.DataFrame([[1, 2, 3],[4, 5, 6],[7,8,9],[10,11,12]],columns=['A', 'B','C'])
#print(df.agg(x=('A', sum), y=('B', min), z=('C', max)))
print(df.agg({'A':["sum"], 'B':["min","max"], 'C':["count"]}))

Once we run the program we will get the following result.


A B C
count NaN NaN 4.0
max NaN 11.0 NaN
min NaN 2.0 NaN
sum 22.0 NaN NaN

Conclusion

In this tutorial, we learned the DataFrame.agg() method which is the python pandas in-built functions. We solved examples by applied this method on DataFrame with single, multiple functions and different functions over the rows and columns.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.