Difference of two columns in pandas dataframe in python is carried out using ” -” operator. Let’s see how to
- Find the difference of two columns in pandas dataframe – python. (subtract one column from other column pandas) with an example.
First let’s create a data frame
import pandas as pd
import numpy as np
#Create a DataFrame
df1 = {
'Name':['George','Andrea','micheal','maggie','Ravi','Xien','Jalpa'],
'Mathematics1_score':[62,47,55,74,32,77,86],
'Mathematics2_score':[45,78,44,89,66,49,72]}
df1 = pd.DataFrame(df1,columns=['Name','Mathematics1_score','Mathematics2_score'])
print(df1)
df1 will be

Difference of two columns in a pandas dataframe in python
Difference of two Mathematical score is computed using simple – operator and stored in the new column namely Score_diff as shown below
df1['Score_diff']=df1['Mathematics1_score'] - df1['Mathematics2_score'] print(df1)
so resultant dataframe will be






