17.9 C
London
Thursday, July 24, 2025
HomePandas in PythonGeneral Functions in PythonHow to Perform a SUMIF Function in Pandas

How to Perform a SUMIF Function in Pandas

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following syntax to find the sum of rows in a pandas DataFrame that meet some criteria:

#find sum of each column, grouped by one column
df.groupby('group_column').sum() 

#find sum of one specific column, grouped by one column
df.groupby('group_column')['sum_column'].sum() 

The following examples show how to use this syntax with the following data frame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['a', 'a', 'b', 'b', 'b', 'c', 'c'],
                   'points': [5, 8, 14, 18, 5, 7, 7],
                   'assists': [8, 8, 9, 3, 8, 7, 4],
                   'rebounds': [1, 2, 2, 1, 0, 4, 1]})

#view DataFrame
df

	team	points	assists	rebounds
0	a	5	8	1
1	a	8	8	2
2	b	14	9	2
3	b	18	3	1
4	b	5	8	0
5	c	7	7	4
6	c	7	4	1

Example 1: Perform a SUMIF Function on One Column

The following code shows how to find the sum of points for each team:

df.groupby('team')['points'].sum()

team
a    13
b    37
c    14

This tells us:

  • Team ‘a’ scored a total of 13 points
  • Team ‘b’ scored a total of 37 points
  • Team ‘c’ scored a total of 14 points

Example 2: Perform a SUMIF Function on Multiple Columns

The following code shows how to find the sum of points and rebounds for each team:

df.groupby('team')[['points', 'rebounds']].sum()

	points	rebounds
team		
a	13	3
b	37	3
c	14	5

Example 3: Perform a SUMIF Function on All Columns

The following code shows how to find the sum of all columns in the data frame for each team:

df.groupby('team').sum()

	points	assists	rebounds
team			
a	13	16	3
b	37	20	3
c	14	11	5

Additional Resources

How to Perform a COUNTIF Function in Pandas
How to Count Observations by Group in Pandas
How to Find the Max Value by Group in Pandas

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories