Difference of two columns in pandas dataframe in Python is carried out by using following methods :
Method #1 : Using ” -” operator.
import pandas as pd # Create a DataFrame df1 = { 'Name':['George','Andrea','micheal', 'maggie','Ravi','Xien','Jalpa'], 'score1':[62,47,55,74,32,77,86], 'score2':[45,78,44,89,66,49,72]} df1 = pd.DataFrame(df1,columns= ['Name','score1','score2']) print("Given Dataframe :\n", df1) # getting Difference df1['Score_diff'] = df1['score1'] - df1['score2'] print("\nDifference of score1 and score2 :\n", df1) |
Given Dataframe :
Name score1 score2
0 George 62 45
1 Andrea 47 78
2 micheal 55 44
3 maggie 74 89
4 Ravi 32 66
5 Xien 77 49
6 Jalpa 86 72
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 maggie 74 89 -15
4 Ravi 32 66 -34
5 Xien 77 49 28
6 Jalpa 86 72 14
Method #2 : Using sub() method of the Dataframe.
import pandas as pd # Create a DataFrame df1 = { 'Name':['George','Andrea','micheal', 'maggie','Ravi','Xien','Jalpa'], 'score1':[62,47,55,74,32,77,86], 'score2':[45,78,44,89,66,49,72]} df1 = pd.DataFrame(df1,columns= ['Name','score1','score2']) print("Given Dataframe :\n", df1) df1['Score_diff'] = df1['score1'].sub(df1['score2'], axis = 0) print("\nDifference of score1 and score2 :\n", df1) |
Given Dataframe :
Name score1 score2
0 George 62 45
1 Andrea 47 78
2 micheal 55 44
3 maggie 74 89
4 Ravi 32 66
5 Xien 77 49
6 Jalpa 86 72
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 maggie 74 89 -15
4 Ravi 32 66 -34
5 Xien 77 49 28
6 Jalpa 86 72 14
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Split a text column into two columns in Pandas DataFrame
- Concatenate two columns of Pandas dataframe
- Sort the Pandas DataFrame by two or more columns
- Python | Delete rows/columns from DataFrame using Pandas.drop()
- How to select multiple columns in a pandas dataframe
- How to drop one or multiple columns in Pandas Dataframe
- How to rename columns in Pandas DataFrame
- Change Data Type for one or more columns in Pandas Dataframe
- Getting frequency counts of a columns in Pandas DataFrame
- Dealing with Rows and Columns in Pandas DataFrame
- Iterating over rows and columns in Pandas DataFrame
- Split a String into columns using regex in pandas DataFrame
- Create a new column in Pandas DataFrame based on the existing columns
- Using dictionary to remap values in Pandas DataFrame columns
- Conditional operation on Pandas DataFrame columns
- Python | Pandas DataFrame.columns
- How to widen output display to see more columns in Pandas dataframe?
- Drop rows from Pandas dataframe with missing values or NaN in columns
- Loop or Iterate over all or certain columns of a dataframe in Python-Pandas
- Get the number of rows and number of columns in Pandas Dataframe
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

