How to Pandas: Subtract Two DataFrames

Pandas provides a convenient way to subtract two DataFrames using the DataFrame.sub() method. This method takes two DataFrames as arguments and subtracts the rows and columns of the first from the second, giving a new DataFrame as the result. This allows for easy subtraction operations between two DataFrames with the same dimensions and column names. The result will be a DataFrame with the same dimensions as the two inputs, but with the values of the first DataFrame subtracted from the second. This makes it easy to compare and analyze two data sets quickly and accurately.


You can use the following syntax to subtract one pandas DataFrame from another:

df1.subtract(df2)

If you have a character column in each DataFrame, you may first need to move it to the index column of each DataFrame:

df1.set_index('char_column').subtract(df2.set_index('char_column'))

The following examples show how to use each syntax in practice.

Example 1: Subtract Two Pandas DataFrames (Numerical Columns Only)

Suppose we have the following two pandas DataFrames that only have numerical columns:

import pandas as pd

#create first DataFrame
df1 = pd.DataFrame({'points': [5, 17, 7, 19, 12, 13, 9, 24],
                    'assists': [4, 7, 7, 6, 8, 7, 10, 11]})

print(df1)

   points  assists
0       5        4
1      17        7
2       7        7
3      19        6
4      12        8
5      13        7
6       9       10
7      24       11

#create second DataFrame
df2 = pd.DataFrame({'points': [4, 22, 10, 3, 7, 8, 12, 10],
                    'assists': [3, 5, 5, 4, 7, 14, 9, 5]})

print(df2)

   points  assists
0       4        3
1      22        5
2      10        5
3       3        4
4       7        7
5       8       14
6      12        9
7      10        5

The following code shows how to subtract the corresponding values between the two DataFrames:

#subtract corresponding values between the two DataFrames
df1.subtract(df2)

	points	assists
0	1	1
1	-5	2
2	-3	2
3	16	2
4	5	1
5	5	-7
6	-3	1
7	14	6

Example 2: Subtract Two Pandas DataFrames (Mix of Character & Numerical Columns)

Suppose we have the following two pandas DataFrames that each have a character column called team:

import pandas as pd

#create first DataFrame
df1 = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                    'points': [5, 17, 7, 19, 12, 13, 9, 24],
                    'assists': [4, 7, 7, 6, 8, 7, 10, 11]})

print(df1)

  team  points  assists
0    A       5        4
1    B      17        7
2    C       7        7
3    D      19        6
4    E      12        8
5    F      13        7
6    G       9       10
7    H      24       11

#create second DataFrame
df2 = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                    'points': [4, 22, 10, 3, 7, 8, 12, 10],
                    'assists': [3, 5, 5, 4, 7, 14, 9, 3]})

print(df2)

  team  points  assists
0    A       4        3
1    B      22        5
2    C      10        5
3    D       3        4
4    E       7        7
5    F       8       14
6    G      12        9
7    H      10        3

The following code shows how to move the team column to the index column of each DataFrame and then subtract the corresponding values between the two DataFrames:

#move 'team' column to index of each DataFrame and subtract corresponding values
df1.set_index('team').subtract(df2.set_index('team'))

	points	assists
team		
A	1	1
B	-5	2
C	-3	2
D	16	2
E	5	1
F	5	-7
G	-3	1
H	14	8

x