How can I perform a SUMIF function in Pandas?

How can I perform a SUMIF function in Pandas?

The SUMIF function in Pandas is a powerful tool that allows users to perform conditional sum calculations on a dataset. This function allows users to specify a specific criteria or condition and then sum up the values that meet that criteria. This can be useful for analyzing and summarizing large datasets. To perform a SUMIF function in Pandas, users need to specify the column or columns to be evaluated, the condition to be met, and the column containing the values to be summed. This function is very similar to the SUMIF function in Excel, making it easy for users familiar with Excel to use in Pandas.

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 columndf.groupby('group_column').sum() 
#find sum of one specific column, grouped by one columndf.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

Cite this article

stats writer (2024). How can I perform a SUMIF function in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-perform-a-sumif-function-in-pandas/

stats writer. "How can I perform a SUMIF function in Pandas?." PSYCHOLOGICAL SCALES, 3 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-perform-a-sumif-function-in-pandas/.

stats writer. "How can I perform a SUMIF function in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-perform-a-sumif-function-in-pandas/.

stats writer (2024) 'How can I perform a SUMIF function in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-perform-a-sumif-function-in-pandas/.

[1] stats writer, "How can I perform a SUMIF function in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I perform a SUMIF function in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top