Pandas: Conditional Sum with Groupby

Learn, how to find the conditional sum for a groupby object?
Submitted by Pranit Sharma, on November 17, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Problem statement

We know how to find out a sum of grouped values, but here we are going to apply a condition and the values which will satisfy the condition will be added together.

We are given a DataFrame with multiple columns. This dataframe represents the data of some students of different courses.

We need to find out the sum of a column where the grouped column is course and we need to apply a condition that only those values will be added where the course is equal to a specific value.

Conditional Sum with Groupby

For this purpose, we will first group the required column, We will then apply a lambda function on the grouped object inside which we will filter the values according to our condition.

Let us understand with the help of an example,

Python program to find the conditional sum for a groupby object

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating a dictionary
d= {

# Creating DataFrame
df = pd.DataFrame(d)

# Display dataframe
print('Original DataFrame:\n',df,'\n')

# Grouping the course column
gp = df.groupby('Course')

# Adding values with a condition
res = gp.apply(lambda x: x[x['Course'] == 'java']['value'].sum())

# Display result


Example: Pandas: Conditional Sum with Groupby

Python Pandas Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.