Articles

agg function in pandas | find aggregate sum in pandas over rows & columns

agg function in pandas | find aggregate sum in pandas over rows & columns


In this pandas tutorial, we will discuss about:

  • agg function in pandas,
  • aggregate sum in pandas over rows,
  • aggregate sum in pandas for specific column,
  • aggregate sum in pandas for all column

Before learning about aggregate sum function in pandas, using agg sum in pandas lets first create a dataframe.

 

Pandas DataFrame is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.

 

We can able to create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.

Syntax:

pandas.DataFrame(data)

Where, data is the input dataframe , The data can be a dictionary that stores list of values with specified key

 

Create DataFrame Example

In this pandas example, we will create a dataframe with 4 rows and 3 columns with building data and assign indices through index parameter.

import pandas as pd

#create dataframe from the building data
data= pd.DataFrame({
                    'length':[5.6,7.8,4.5,5.3],

                   "breadth":[12.9,4.5,21.5,6.0],

                    "area":[20,56,43,45]

                   },index=['one','two','three','four'])

#display the dataframe
print(data)

Output:

       length  breadth  area
one       5.6     12.9    20
two       7.8      4.5    56
three     4.5     21.5    43
four      5.3      6.0    45

Thus we have created our dataframe. Now lets learn about aggregate sum function in pandas.


aggregate sum function in pandas

If we want to get the total sum of the columns in the dataframe, then we have to use aggregate sum function in pandas.

 

In that, sum is one of the function, which will return the total value (row/column) of the dataframe.

For aggregation, the method used is agg in pandas and for sum, the method used is sum.

There are three scenarios of finding aggregate sum function in pandas using agg function in pandasLet's discuss one by one.


Scenario 1 : aggregate sum in pandas over the rows

In this aggregate sum in pandas scenario, we will get the sum over each row in the dataframe.

Syntax:

dataframe.agg('sum')

where, dataframe is the input dataframe.

 

Exampleaggregate sum in pandas over the rows

Lets see program on agg function in pandas to get aggregate sum over rows

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({
                    'length':[5.6,7.8,4.5,5.3],

                   "breadth":[12.9,4.5,21.5,6.0],

                    "area":[20,56,43,45]

                   },index=['one','two','three','four'])

#sum aggregation over the rows
data.agg('sum')

Output: From the above aggregate sum in pandas example, we will perform sum aggregation over the rows

length      23.2
breadth     44.9
area       164.0
dtype: float64

Lets see scenario two on aggregate sum in pandas using agg in pandas.


Scenario 2 : sum aggregation per column

In this aggregate sum in pandas scenario, we will get the sum for the mentioned columns in the dataframe.

agg in pandas Syntax:

dataframe.agg({'column' : 'sum', ..............})

where, dataframe is the input dataframe and column is the column name to get sum.

 

Exampleaggregate sum in pandas per column

In this aggregate sum in pandas example, we will get sum for length and area columns.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({
                    'length':[5.6,7.8,4.5,5.3],

                   "breadth":[12.9,4.5,21.5,6.0],

                    "area":[20,56,43,45]

                   },index=['one','two','three','four'])

#sum aggregation per column
data.agg({'length' : 'sum', 'area' : 'sum'})

OutputIn the above aggregate sum in pandas example, we will perform sum aggregation for length and area columns.

length     23.2
area      164.0
dtype: float64

Lets see third scenario on aggregate sum function in pandas using agg in pandas.


Scenario 3 : sum aggregation over the columns

In this aggregate sum in pandas scenario, we will get the sum over each column in the dataframe.

Syntax:

dataframe.agg("sum", axis="columns")

where, dataframe is the input dataframe.

 

Exampleaggregate sum in pandas over each columns

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({
                    'length':[5.6,7.8,4.5,5.3],

                   "breadth":[12.9,4.5,21.5,6.0],

                    "area":[20,56,43,45]

                   },index=['one','two','three','four'])

# aggregation over the columns
data.agg("sum", axis="columns")

OutputFrom the above aggregate sum in pandas example, we will perform sum aggregation over the columns

one      38.5
two      68.3
three    69.0
four     56.3
dtype: float64

This is how we can use aggregate sum function in pandas using agg in pandas.


Pandas

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 800+ Technical Articles on Python, R, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Apr 22,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!