Pandas DataFrame.mul
Pandas DataFrame.mul
The DataFrame.mul
method in pandas is used to perform element-wise multiplication of a DataFrame with another DataFrame, Series, or scalar value. This method provides flexible options to handle mismatched data and axes.
Syntax
The syntax for DataFrame.mul
is:
DataFrame.mul(other, axis='columns', level=None, fill_value=None)
Here, DataFrame
refers to the pandas DataFrame being multiplied.
Parameters
Parameter | Description |
---|---|
other | The object to multiply with, which can be a DataFrame, Series, or scalar value. |
axis | Determines the alignment axis. Can be 'index' (0) or 'columns' (1). Defaults to 'columns' . |
level | Used to broadcast along a particular level, matching index values on a MultiIndex. |
fill_value | Value to fill in for missing data before performing multiplication. By default, no filling is performed. |
Returns
A DataFrame resulting from the element-wise multiplication.
Examples
Multiplying by a Scalar
Multiply all elements in a DataFrame by a scalar value.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Product A': [10, 20, 30],
'Product B': [15, 25, 35]
}
df = pd.DataFrame(data)
# Multiply by a scalar
print("DataFrame after multiplying by 2:")
df_scaled = df.mul(2)
print(df_scaled)
Output
DataFrame after multiplying by 2:
Product A Product B
0 20 30
1 40 50
2 60 70
Element-Wise Multiplication with Another DataFrame
Perform element-wise multiplication with another DataFrame.
Python Program
import pandas as pd
# Create two DataFrames
data1 = {
'Product A': [10, 20, 30],
'Product B': [15, 25, 35]
}
data2 = {
'Product A': [1, 2, 3],
'Product B': [0.5, 1, 1.5]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
# Element-wise multiplication
print("Element-wise multiplication:")
df_multiplied = df1.mul(df2)
print(df_multiplied)
Output
Element-wise multiplication:
Product A Product B
0 10.0 7.5
1 40.0 25.0
2 90.0 52.5
Multiplication with a Series
Multiply a DataFrame with a Series along a specified axis.
Python Program
import pandas as pd
# Create a DataFrame and a Series
data = {
'Product A': [10, 20, 30],
'Product B': [15, 25, 35]
}
multipliers = pd.Series([1, 2, 3], index=[0, 1, 2])
df = pd.DataFrame(data)
# Multiply with Series along the index
print("Multiplication with a Series:")
df_multiplied = df.mul(multipliers, axis='index')
print(df_multiplied)
Output
Multiplication with a Series:
Product A Product B
0 10 15.0
1 40 50.0
2 90 105.0
Using fill_value
for Missing Data
Use the fill_value
parameter to handle missing data before multiplication.
Python Program
import pandas as pd
# Create two DataFrames with missing values
data1 = {
'Product A': [10, None, 30],
'Product B': [15, 25, None]
}
data2 = {
'Product A': [1, 2, 3],
'Product B': [0.5, None, 1.5]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
# Multiply with fill_value
print("Multiplication with fill_value=1:")
df_multiplied = df1.mul(df2, fill_value=1)
print(df_multiplied)
Output
Multiplication with fill_value=1:
Product A Product B
0 10.0 7.5
1 2.0 25.0
2 90.0 1.5
Summary
In this tutorial, we explored the DataFrame.mul
method in pandas. Key takeaways include:
- Performing element-wise multiplication with DataFrames, Series, or scalar values.
- Using the
axis
parameter to control alignment for multiplication with Series. - Handling missing data with the
fill_value
parameter.
The DataFrame.mul
method is a flexible tool for performing element-wise multiplication in pandas, making it ideal for data analysis tasks.