How to Perform a SUMIF Function in Pandas


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

Leave a Reply

Your email address will not be published.