Pandas DataFrame.rmul
Pandas DataFrame.rmul
The DataFrame.rmul
method in pandas performs element-wise multiplication of another value (or DataFrame/Series) with the calling DataFrame, with reverse operation priority. This is useful when you need to reverse the order of multiplication, such as when the scalar or other operand is on the left-hand side.
Syntax
The syntax for DataFrame.rmul
is:
DataFrame.rmul(other, axis='columns', level=None, fill_value=None)
Here, DataFrame
refers to the pandas DataFrame on which the method is called.
Parameters
Parameter | Description |
---|---|
other | The value, Series, or DataFrame to be multiplied with the calling DataFrame. |
axis | Axis to broadcast over. Can be 'index' (0) or 'columns' (1). Defaults to 'columns' . |
level | If the axis is a MultiIndex, this parameter specifies the level to match for alignment. Defaults to None . |
fill_value | Value to fill in for missing data on either side during the operation. Defaults to None . |
Returns
A DataFrame with the reverse element-wise multiplication applied.
Examples
Basic Reverse Multiplication with a Scalar
Use rmul
to perform reverse multiplication with a scalar.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'A': [1, 2, 3],
'B': [4, 5, 6]
}
df = pd.DataFrame(data)
# Reverse multiply with a scalar
print("Reverse multiplication with scalar 10:")
result = 10 * df
print(result)
Output
Reverse multiplication with scalar 10:
A B
0 10 40
1 20 50
2 30 60
Reverse Multiplication with Another DataFrame
Use rmul
to reverse multiply with another DataFrame of the same shape.
Python Program
import pandas as pd
# Create two DataFrames
data1 = {
'A': [1, 2, 3],
'B': [4, 5, 6]
}
data2 = {
'A': [10, 20, 30],
'B': [40, 50, 60]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
# Reverse multiply
print("Reverse multiplication with another DataFrame:")
result = df2.rmul(df1)
print(result)
Output
Reverse multiplication with another DataFrame:
A B
0 10 160
1 40 250
2 90 360
Reverse Multiplication with a Series
Use rmul
to reverse multiply with a Series along a specified axis.
Python Program
import pandas as pd
# Create a DataFrame and a Series
data = {
'A': [1, 2, 3],
'B': [4, 5, 6]
}
df = pd.DataFrame(data)
series = pd.Series([10, 20, 30])
# Reverse multiply along index axis
print("Reverse multiplication with a Series along index:")
result = series.rmul(df, axis=0)
print(result)
Output
Reverse multiplication with a Series along index:
A B
0 10 40
1 40 100
2 90 180
Using fill_value
for Missing Data
Use the fill_value
parameter to specify a value for filling missing data during the operation.
Python Program
import pandas as pd
# Create two DataFrames with missing values
data1 = {
'A': [1, 2, None],
'B': [4, None, 6]
}
data2 = {
'A': [10, 20, 30],
'B': [40, 50, None]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
# Reverse multiply with fill_value
print("Reverse multiplication with fill_value=1:")
result = df2.rmul(df1, fill_value=1)
print(result)
Output
Reverse multiplication with fill_value=1:
A B
0 10.0 160.0
1 40.0 50.0
2 30.0 6.0
Summary
In this tutorial, we explored the DataFrame.rmul
method in pandas. Key takeaways include:
- Using
rmul
for reverse element-wise multiplication with scalars, Series, or DataFrames. - Specifying an axis for alignment during the operation.
- Handling missing data with the
fill_value
parameter.
The DataFrame.rmul
method is a versatile tool for reverse multiplication in pandas.