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.

Conditional Sum with Groupby

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.

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 code 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= {
    'id':[100,100,101,101,102,102],
    'Course':['java','python','java','python','c++','c++'],
    'value':[10,20,30,40,50,60]
}

# 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
print("Result:\n",res)

Output:

Example: Pandas: Conditional Sum with Groupby

Python Pandas Programs »



ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.