SoFunction
Updated on 2025-04-26

pandas DataFrame rsub implementation example

Pandas2.2 DataFrame

Binary operator functions

method describe
DataFrame.add(other) Used to perform element-by-element addition of DataFrame with another object such as DataFrame, Series, or scalar
(other[, axis, level, fill_value]) Used to perform element-by-element addition of DataFrame with another object such as DataFrame, Series, or scalar
(other[, axis, level, fill_value]) Used to perform element-by-element subtraction operations
(other[, axis, level, fill_value]) Used to perform element-by-element multiplication operations
(other[, axis, level, fill_value]) Used to perform element-by-element division operations
(other[, axis, level, …]) Used to perform element-by-element true division operation
(other[, axis, level, …]) Used to perform element-by-element floor division operation
(other[, axis, level, fill_value]) Used to perform element-by-element modulation operation
(other[, axis, level, fill_value]) Used to exponentate elements in DataFrame
(other) Method for calculating the matrix dot product (matrix multiplication) between two DataFrames (or DataFrame and Series/arrays)
(other[, axis, level, fill_value]) Used to perform reverse addition operation
(other[, axis, level, fill_value]) Used to perform reverse subtraction operations

()

Methods are used to perform reverse subtraction operations. Specifically, it is equivalent to callingother - self,inselfis the DataFrame that calls this method. The following are the parameters and functions of this method:

Parameter description

  • other: The value used for subtraction operations can be a scalar, a sequence, a DataFrame, or a dictionary.
  • axis: Specifies which axis to perform the operation.0or'index'Indicates that the operation is performed along the line.1or'columns'Indicates that operations are performed along the column. Default is1
  • level: ifotheris a MultiIndex, which specifies the level to perform operations. Default isNone
  • fill_value: The value used to fill in missing values. Default isNone

Examples and results

Example 1: Inverse subtraction using scalars

import pandas as pd

df = ({
    'A': [1, 2, 3],
    'B': [4, 5, 6],
    'C': [7, 8, 9]
})

print("Original DataFrame:")
print(df)

result = (10)
print("\nAfter reverse subtraction DataFrame (use rsub and specify scalar 10):")
print(result)

result:

Original DataFrame:
   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9

DataFrame after reverse subtraction (using rsub and specifying scalar 10):
    A   B   C
0   9   6   3
1   8   5   2
2   7   4   1

Example 2: Inverse subtraction using sequences

import pandas as pd

df = ({
    'A': [1, 2, 3],
    'B': [4, 5, 6],
    'C': [7, 8, 9]
})

other = ([1, 2, 3])

print("Original DataFrame:")
print(df)

result = (other, axis=0)
print("\nAfter reverse subtraction DataFrame (use rsub and specify the sequence):")
print(result)

result:

Original DataFrame:
   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9

DataFrame after reverse subtraction (using rsub and specifying the sequence):
    A   B   C
0   0  -3  -6
1   0  -3  -6
2   0  -3  -6

Example 3: Inverse subtraction using DataFrame

import pandas as pd

df = ({
    'A': [1, 2, 3],
    'B': [4, 5, 6],
    'C': [7, 8, 9]
})

other_df = ({
    'A': [1, 2, 3],
    'B': [4, 5, 6],
    'C': [7, 8, 9]
})

print("Original DataFrame:")
print(df)

result = (other_df)
print("\nAfter reverse subtraction DataFrame (use rsub and specify DataFrame):")
print(result)

result:

Original DataFrame:
   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9

DataFrame after reverse subtraction (using rsub and specifying DataFrame):
   A  B  C
0  0  0  0
1  0  0  0
2  0  0  0

Example 4: Inverse subtraction using dictionary

import pandas as pd

df = ({
    'A': [1, 2, 3],
    'B': [4, 5, 6],
    'C': [7, 8, 9]
})

other_dict = {'A': 1, 'B': 2, 'C': 3}

print("Original DataFrame:")
print(df)

result = (other_dict)
print("\nAfter reverse subtraction DataFrame (use rsub and specify a dictionary):")
print(result)

result:

Original DataFrame:
   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9

DataFrame after reverse subtraction (using rsub and specifying dictionary):
   A  B  C
0  0 -2 -4
1  0 -3 -5
2  0 -4 -6

explain

  • Reverse subtraction using scalar:

    • (10)Calculate DataFramedfEach element in   and scalar10subtraction.
    • The result is a new DataFrame where each element is10Subtractdfelements in  .
  • Reverse subtraction using sequence:

    • (other, axis=0)Calculate DataFramedfEach row and sequence ofotherSubtraction of the corresponding elements.
    • The result is a new DataFrame where each element isotherminus the corresponding elementdfelements.
  • Reverse subtraction using DataFrame:

    • (other_df)Calculate DataFramedfandother_dfSubtraction of the corresponding elements.
    • The result is a new DataFrame where each element isother_dfElement minusdfelements.
  • Inverse subtraction using dictionary:

    • (other_dict)Calculate DataFramedfEach column and dictionaryother_dictSubtraction of the values ​​of the corresponding keys in  .
    • The result is a new DataFrame where each element is a dictionaryother_dictminus the value indfelements.

These examples showDifferent usages of methods and their effects. Depending on the specific requirements, appropriate parameters can be selected to perform reverse subtraction operations.

This is the end of this article about the implementation example of pandas DataFrame rsub. For more related pandas DataFrame rsub content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!