Open In App

Difference of two columns in Pandas dataframe

Last Updated : 03 Oct, 2025
Comments
Improve
Suggest changes
38 Likes
Like
Report

In Python, the difference between two columns in a Pandas DataFrame can be calculated using multiple methods. Here, we will discuss two simple approaches:

  1. Using "-" difference/subtract operator
  2. Using sub() method.

Method 1: Using the "-" Operator

You can directly subtract one column from another using the "-" operator to create a new column.

Python
import pandas as pd

# Create a DataFrame
df = { 'Name':['George','Andrea','micheal',
                'maggie','Ravi','Xien','Jalpa'],
        'score1':[62,47,55,74,32,77,86],
        'score2':[45,78,44,89,66,49,72]}

df = pd.DataFrame(df,columns= ['Name','score1','score2'])

# getting Difference
df['Score_diff'] = df1['score1'] - df['score2']
print("\nDifference of score1 and score2 :\n", df)

Output
Difference of score1 and score2 :
       Name  score1  score2  Score_diff
0   George      62      45          17
1   Andrea      47      78         -31
2  micheal      55      44          11
3   magg...

Method 2: Using the sub() Method

Pandas provides a built-in method sub() to subtract one column from another. It is particularly useful when performing element-wise subtraction with alignment along a particular axis.

Python
import pandas as pd

# Create a DataFrame
df = { 'Name':['George','Andrea','micheal',
                'maggie','Ravi','Xien','Jalpa'],
        'score1':[62,47,55,74,32,77,86],
        'score2':[45,78,44,89,66,49,72]}
 
df = pd.DataFrame(df,columns= ['Name','score1','score2'])
 
df['Score_diff'] = df['score1'].sub(df['score2'], axis = 0)
print("\nDifference of score1 and score2 :\n", df)

Output
Difference of score1 and score2 :
       Name  score1  score2  Score_diff
0   George      62      45          17
1   Andrea      47      78         -31
2  micheal      55      44          11
3   magg...

Explore