Pandas DataFrame.div
Pandas DataFrame.div
The DataFrame.div method in pandas is used to perform element-wise division of a DataFrame by another DataFrame, Series, or scalar. This method is flexible and allows division along rows or columns.
Syntax
The syntax for DataFrame.div is:
DataFrame.div(other, axis='columns', level=None, fill_value=None)Here, DataFrame refers to the pandas DataFrame that is being divided.
Parameters
| Parameter | Description |
|---|---|
other | The object to divide by. Can be a scalar, Series, or DataFrame. |
axis | The axis to align the other object with: 'columns' (default) or 'index'. |
level | If the axis is a MultiIndex, this specifies the level to broadcast over. |
fill_value | Value to fill in missing entries before division. By default, missing values result in NaN. |
Returns
A DataFrame with element-wise division results.
Examples
Dividing by a Scalar
Divide all elements of a DataFrame by a scalar value.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'A': [10, 20, 30],
'B': [40, 50, 60],
'C': [70, 80, 90]
}
df = pd.DataFrame(data)
# Divide the DataFrame by a scalar
print("Dividing DataFrame by 10:")
df_divided = df.div(10)
print(df_divided)Output
Dividing DataFrame by 10:
A B C
0 1.0 4.0 7.0
1 2.0 5.0 8.0
2 3.0 6.0 9.0Dividing by Another DataFrame
Divide one DataFrame by another DataFrame element-wise.
Python Program
import pandas as pd
# Create two DataFrames
data1 = {
'A': [10, 20, 30],
'B': [40, 50, 60],
'C': [70, 80, 90]
}
data2 = {
'A': [2, 4, 6],
'B': [8, 10, 12],
'C': [14, 16, 18]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
# Element-wise division
print("Dividing one DataFrame by another:")
df_divided = df1.div(df2)
print(df_divided)Output
Dividing one DataFrame by another:
A B C
0 5.0 5.0 5.0
1 5.0 5.0 5.0
2 5.0 5.0 5.0Dividing with Alignment Along Rows
Divide a DataFrame by a Series along rows (axis=0).
Python Program
import pandas as pd
# Create a DataFrame
data = {
'A': [10, 20, 30],
'B': [40, 50, 60],
'C': [70, 80, 90]
}
df = pd.DataFrame(data)
# Create a Series to divide
s = pd.Series([10, 20, 30], index=[0, 1, 2])
# Perform division along rows
print("Dividing DataFrame by a Series along rows:")
df_divided = df.div(s, axis=0)
print(df_divided)Output
Dividing DataFrame by a Series along rows:
A B C
0 1.0 4.0 7.0
1 1.0 2.5 4.0
2 1.0 2.0 3.0Handling Missing Values with fill_value
Use the fill_value parameter to fill missing values before division.
Python Program
import pandas as pd
# Create two DataFrames with missing values
data1 = {
'A': [10, None, 30],
'B': [40, 50, None],
'C': [70, 80, 90]
}
data2 = {
'A': [2, 4, 6],
'B': [8, 10, 12],
'C': [14, 16, 18]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
# Element-wise division with fill_value
print("Dividing with fill_value=1:")
df_divided = df1.div(df2, fill_value=1)
print(df_divided)Output
Dividing with fill_value=1:
A B C
0 5.0 5.0 5.0
1 0.0 5.0 5.0
2 5.0 0.0 5.0Summary
In this tutorial, we explored the DataFrame.div method in pandas. Key takeaways include:
- Performing scalar division on all DataFrame elements.
- Dividing DataFrames element-wise.
- Using alignment along rows or columns.
- Handling missing values with the
fill_valueparameter.
The DataFrame.div method is a powerful tool for applying element-wise division in pandas DataFrames.