Pandas DataFrame.rsub
Pandas DataFrame.rsub
The DataFrame.rsub method in pandas performs the reverse subtraction of a DataFrame and another object (scalar, Series, or DataFrame). This means it subtracts the values in the DataFrame from the other object.
Syntax
The syntax for DataFrame.rsub is:
DataFrame.rsub(other, axis='columns', level=None, fill_value=None)Here, DataFrame refers to the pandas DataFrame involved in the operation.
Parameters
| Parameter | Description |
|---|---|
other | The object to be subtracted from (scalar, Series, or DataFrame). |
axis | Determines the axis for the operation. Defaults to 'columns' (1). Can be 0 or 'index' for row-wise operation. |
level | If the axis is a MultiIndex (hierarchical), this specifies the level to broadcast along. Defaults to None. |
fill_value | Value to substitute for missing data. Defaults to None. |
Returns
A DataFrame where each element is the result of the reverse subtraction.
Examples
Reverse Subtraction with a Scalar
Subtract each value in the DataFrame from a scalar.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]
}
df = pd.DataFrame(data)
# Perform reverse subtraction with a scalar
result = df.rsub(10)
print("Result of reverse subtraction with scalar 10:")
print(result)Output
Result of reverse subtraction with scalar 10:
A B C
0 9 6 3
1 8 5 2
2 7 4 1Reverse Subtraction with Another DataFrame
Perform reverse subtraction between two DataFrames.
Python Program
import pandas as pd
# Create two DataFrames
data1 = {
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]
}
data2 = {
'A': [10, 20, 30],
'B': [40, 50, 60],
'C': [70, 80, 90]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
# Perform reverse subtraction
result = df1.rsub(df2)
print("Result of reverse subtraction between DataFrames:")
print(result)Output
Result of reverse subtraction between DataFrames:
A B C
0 9 36 63
1 18 45 72
2 27 54 81Reverse Subtraction with a Series
Perform reverse subtraction between a DataFrame and a Series.
Python Program
import pandas as pd
# Create a DataFrame and a Series
data = {
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]
}
df = pd.DataFrame(data)
series = pd.Series([10, 20, 30], index=['A', 'B', 'C'])
# Perform reverse subtraction
result = df.rsub(series, axis='columns')
print("Result of reverse subtraction with a Series:")
print(result)Output
Result of reverse subtraction with a Series:
A B C
0 9 16 23
1 8 15 22
2 7 14 21Using fill_value to Handle Missing Data
Use the fill_value parameter to handle missing data during reverse subtraction.
Python Program
import pandas as pd
# Create two DataFrames with missing data
data1 = {
'A': [1, 2, None],
'B': [4, 5, 6]
}
data2 = {
'A': [10, 20, 30],
'B': [None, 50, 60]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
# Perform reverse subtraction with fill_value
result = df1.rsub(df2, fill_value=0)
print("Result of reverse subtraction with fill_value=0:")
print(result)Output
Result of reverse subtraction with fill_value=0:
A B
0 9.0 -4.0
1 18.0 45.0
2 30.0 54.0Summary
In this tutorial, we explored the DataFrame.rsub method in pandas. Key takeaways include:
- Performing reverse subtraction with scalars, Series, or other DataFrames.
- Using the
fill_valueparameter to handle missing data. - Specifying the
axisparameter for column-wise or row-wise operations.
The DataFrame.rsub method is a useful tool for performing reverse subtraction operations in pandas.