Pandas DataFrame Subtraction: sub() function

Python pandas library provides multitude of functions to work on two dimensioanl Data through the DataFrame class.

The sub() method of pandas DataFrame subtracts the elements of one DataFrame from the elements of another DataFrame. Invoking sub() method on a DataFrame object is equivalent to calling the binary subtraction operator(-).

The sub() method supports passing a parameter for missing values(np.nan, None). The subtract function of pandas is used to perform subtract operation on dataframes.

Syntax

pandas.DataFrame.sub(other, axis=’columns’, level=None, fill_value=None)

  • other : scalar, sequence, Series, or DataFrame – This parameter consists any single or multiple element data structure, or list-like object.
  • axis : {0 or ‘index’, 1 or ‘columns’} – This is used for deciding the axis on which the operation is applied.
  • level : int or label – The level parameter is used for broadcasting across a level and matching Index values on the passed MultiIndex level.
  • fill_value : float or None, default None – Whenever the dataframes have missing values, then to fill existing missing (NaN) values, we can use fill_value parameter.

The function will output the result of subtract function.

Example 1: Subtraction using pandas sub()

In this example, an array is provided to the subtract function of pandas. The axis parameter is provided to specify the axis on which the operation is performed. We can see in the output that the values are decreasing in the dataframe.

Input:

df

Output:

 speedweight
Audi80250
Jaguar90200
BMW110150

Input:

df.sub([15, 30], axis='columns')

Output:

 speedweight
Audi65220
Jaguar75170
BMW95120

Example 2: Using series data along with pandas subtraction function

In the 2nd example, series data is passed to the pandas sub() function. By providing the correct index, the values specified in the series data is deducted from the dataframe.

Input:

df

Output:

 speedweight
Audi80250
Jaguar90200
BMW110150

Input:

df.sub(pd.Series([7, 9, 11], index=['Audi', 'Jaguar', 'BMW']),
        axis='index')

Output:

 speedweight
Audi73243
Jaguar81191
BMW99139

Example 3: Subtraction using scalar values

Here scalar values are used for performing subtraction operation without using pandas subtraction function.

In [14]:

df

Output:

 speedweight
Audi80250
Jaguar90200
BMW110150

Input:

df - [6, 9]

Output:

 speedweight
Audi74241
Jaguar84191
BMW104141

Example: 

input:

import pandas as pd

 

# Create Data

data1 = [(2, 4, 6, 8),

         (1, 3, 5, 7),

         (5, 0, 0, 9)];

 

data2 = [(1, 1, 0 , 1),

         (1, 0, 1 , 1),

         (0, 1, 1 , 0)];

 

# Construct DataFrame1        

dataFrame1 = pd.DataFrame(data=data1);

print("DataFrame1:");

print(dataFrame1);

 

# Construct DataFrame2

dataFrame2 = pd.DataFrame(data=data2);

print("DataFrame2:");

print(dataFrame2);

 

# Subtracting DataFrame2 from DataFrame1

subtractionResults = dataFrame1 - dataFrame2;

print("Result of subtracting dataFrame1 from dataFrame2:");

print(subtractionResults);

output:

DataFrame1:

   0  1  2  3

0  2  4  6  8

1  1  3  5  7

2  5  0  0  9

DataFrame2:

   0  1  2  3

0  1  1  0  1

1  1  0  1  1

2  0  1  1  0

Result of subtracting dataFrame1 from dataFrame2:

   0  1  2  3

0  1  3  6  7

1  0  3  4  6

2  5 -1 -1  9
Tags