How to sum values in a column that matches a given condition using Pandas?

Given a Pandas DataFrame, we have to sum values in a column that matches a given condition.
Submitted by Pranit Sharma, on July 11, 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

Suppose we have a column with integer values and another column with strings and we want the values of the 2nd column to be modified based on the 1st column let us say we need to add all those values of column 2 where the corresponding value of 1st column is 5.

Find sum values in a Pandas column that matches a given condition

To find the sum value in a column that matches a given condition, we will use pandas.DataFrame.loc property and sum() method, first, we will check the condition if the value of 1st column matches a specific condition, then we will collect these values and apply the sum() method.

Note

To work with pandas, we need to import pandas package first, below is the syntax:

```import pandas as pd
```

Let us understand with the help of an example,

Python program to sum values in a column that matches a given condition using Pandas

```# Importing pandas package
import pandas as pd

# Creating a dictionary
d= {
'Col_1':[1,5,10,1,5,10,1,5,10,1,5,10],
'Col_2':[10,20,30,40,50,60,70,80,90,100,110,120]
}

# Creating a DataFrame
df = pd.DataFrame(d)

# Display original DataFrame
print("Original DataFrame:\n",df,"\n")

# Performing our operation
result = df.loc[df['Col_1'] == 1, 'Col_2'].sum()

# Display result
print("Result,\n",result)
```