Pandas DataFrame.truediv


Pandas DataFrame.truediv

The DataFrame.truediv method in pandas is used to perform element-wise true division (division resulting in floating-point numbers) between a DataFrame and another DataFrame, Series, or scalar. This method is part of the arithmetic operations provided by pandas for mathematical computations.


Syntax

The syntax for DataFrame.truediv is:

DataFrame.truediv(other, axis='columns', level=None, fill_value=None)

Here, DataFrame refers to the pandas DataFrame being divided.


Parameters

ParameterDescription
otherThe object to divide by. Can be a DataFrame, Series, or scalar value.
axisDetermines the alignment axis. 'columns' (default) divides along columns, and 'index' divides along rows.
levelIf the axis is a MultiIndex, specify the level to align on.
fill_valueValue to use to fill missing data for alignment. Defaults to None.

Returns

A new DataFrame with the result of the element-wise true division.


Examples

Dividing a DataFrame by a Scalar

Use truediv to divide all elements in a DataFrame by a scalar.

Python Program

import pandas as pd

# Create a DataFrame
data = {
    'A': [10, 20, 30],
    'B': [40, 50, 60]
}
df = pd.DataFrame(data)

# Divide all elements by a scalar
print("Dividing DataFrame by 10:")
result = df.truediv(10)
print(result)

Output

Dividing DataFrame by 10:
     A    B
0  1.0  4.0
1  2.0  5.0
2  3.0  6.0

Dividing Two DataFrames

Divide two DataFrames element-wise. Missing values can be handled using the fill_value parameter.

Python Program

import pandas as pd

# Create two DataFrames
data1 = {
    'A': [10, 20, 30],
    'B': [40, 50, 60]
}
data2 = {
    'A': [2, 4, 6],
    'B': [8, 10, 12]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)

# Divide df1 by df2
print("Dividing df1 by df2:")
result = df1.truediv(df2)
print(result)

Output

Dividing df1 by df2:
     A    B
0  5.0  5.0
1  5.0  5.0
2  5.0  5.0

Using fill_value for Missing Data

Handle missing data by specifying a fill_value.

Python Program

import pandas as pd

# Create two DataFrames with mismatched columns
data1 = {
    'A': [10, 20, 30],
    'B': [40, 50, 60]
}
data2 = {
    'A': [2, 4, 6],
    'C': [8, 10, 12]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)

# Divide with a fill_value
print("Dividing df1 by df2 with fill_value=1:")
result = df1.truediv(df2, fill_value=1)
print(result)

Output

Dividing df1 by df2 with fill_value=1:
      A     B     C
0   5.0  40.0   5.0
1   5.0  50.0  20.0
2   5.0  60.0  30.0

Dividing a DataFrame by a Series

Use truediv to divide a DataFrame by a Series along a specified axis.

Python Program

import pandas as pd

# Create a DataFrame and a Series
data = {
    'A': [10, 20, 30],
    'B': [40, 50, 60]
}
series = pd.Series([2, 4, 6], index=['A', 'B', 'C'])
df = pd.DataFrame(data)

# Divide DataFrame by Series
print("Dividing DataFrame by Series along columns:")
result = df.truediv(series, axis='columns')
print(result)

Output

Dividing DataFrame by Series along columns:
      A     B
0   5.0  10.0
1  10.0  12.5
2  15.0  15.0

Summary

In this tutorial, we explored the DataFrame.truediv method in pandas. Key takeaways include:

  • Performing element-wise true division with scalars, DataFrames, and Series.
  • Handling missing data with the fill_value parameter.
  • Using the axis parameter to control the alignment of the operation.

The DataFrame.truediv method is a flexible and powerful tool for performing division operations in pandas.


Python Libraries