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 calling
other - self
,inself
is 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.
0
or'index'
Indicates that the operation is performed along the line.1
or'columns'
Indicates that operations are performed along the column. Default is1
。 -
level: if
other
is a MultiIndex, which specifies the level to perform operations. Default isNone
。 -
fill_value: The value used to fill in missing values. Default is
None
。
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 9DataFrame 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 9DataFrame 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 9DataFrame 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 9DataFrame 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 DataFramedf
Each element in and scalar10
subtraction. - The result is a new DataFrame where each element is
10
Subtractdf
elements in .
-
-
Reverse subtraction using sequence:
-
(other, axis=0)
Calculate DataFramedf
Each row and sequence ofother
Subtraction of the corresponding elements. - The result is a new DataFrame where each element is
other
minus the corresponding elementdf
elements.
-
-
Reverse subtraction using DataFrame:
-
(other_df)
Calculate DataFramedf
andother_df
Subtraction of the corresponding elements. - The result is a new DataFrame where each element is
other_df
Element minusdf
elements.
-
-
Inverse subtraction using dictionary:
-
(other_dict)
Calculate DataFramedf
Each column and dictionaryother_dict
Subtraction of the values of the corresponding keys in . - The result is a new DataFrame where each element is a dictionary
other_dict
minus the value indf
elements.
-
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!