SoFunction
Updated on 2025-04-23

The specific use of pandas DataFrame truediv

Pandas2.2 DataFrame

Binary operator functions

method describe
DataFrame.add(other) Used to perform element-by-element addition of DataFrame with another object such as DataFrame, Series, or scalar
(other[, axis, level, fill_value]) Used to perform element-by-element addition of DataFrame with another object such as DataFrame, Series, or scalar
(other[, axis, level, fill_value]) Used to perform element-by-element subtraction operations
(other[, axis, level, fill_value]) Used to perform element-by-element multiplication operations
(other[, axis, level, fill_value]) Used to perform element-by-element division operations
(other[, axis, level, …]) Used to perform element-by-element true division operation

()

()Methods are used to perform element-by-element true division operations. True division refers to the use of floating-point division. Even if both operands are integers, the result will be floating-point numbers. This method can be used for division between two DataFrames, or for division between DataFrame and a scalar. The following is a detailed description of the parameters:

  • other: Can be another DataFrame, Series, Index, constant, or an array that can be broadcast to the same shape.
  • axis: Specifies which axis to operate along.0or'index'Indicates operation along the line,1or'columns'Indicates operation along the column. Default is'columns'
  • level: If the index is a multi-index, you can specify which level to operate along. Default isNone
  • fill_value: If a missing value (NaN) is encountered, you can use this value to fill it. Default isNone

Example

Suppose we have two DataFrames:

import pandas as pd

df1 = ({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})

df2 = ({
    'A': [1, 1, 1],
    'B': [2, 2, 2]
})

Example 1: True division between DataFrame and DataFrame

result = (df2)
print(result)

Output:

     A    B
0  1.0  2.0
1  2.0  2.5
2  3.0  3.0

Example 2: True division between DataFrame and scalar

result = (2)
print(result)

Output:

   A  B
0  0.5  2.0
1  1.0  2.5
2  1.5  3.0

Example 3: Use fill_value to handle missing values

Assumptiondf2There is a missing value:

[0, 0] = None  # Set a value in df2 to NaNresult = (df2, fill_value=1)
print(result)

Output:

     A    B
0  1.0  2.0
1  2.0  2.5
2  3.0  3.0

In this example,df2The first element in it isNaN,usefill_value=1back,df1Corresponding elements in1Divided by1, the result is still1

Example 4: Handling the case of dividing by zero

ifdf2There is a zero value in  , the result will beinfor-inf

[1, 1] = 0  # Set a value in df2 to 0result = (df2)
print(result)

Output:

     A   B
0  1.0  2.0
1  2.0   inf
2  3.0  3.0

In this example,df2The second element in it is0df1Corresponding elements in5Divided by0,turn outinf

Example 5: Specify axis parameter

Suppose we have a DataFrame and a Series, which can be specified byaxisParameters to control the axis of division operation:

series_row = ([100, 200, 300], index=[0, 1, 2])
result_axis_0 = (series_row, axis=0)
print("\nDataFrame 1 / Series (axis=0):")
print(result_axis_0)

series_col = ([10, 20], index=['A', 'B'])
result_axis_1 = (series_col, axis=1)
print("\nDataFrame 1 / Series (axis=1):")
print(result_axis_1)

Output:

DataFrame 1 / Series (axis=0):
      A     B
0  0.01  0.02
1  0.01  0.025
2  0.01  0.02

DataFrame 1 / Series (axis=1):
    A    B
0  0.1  0.2
1  0.2  0.25
2  0.3  0.3

Summarize

()Methods provide a flexible way to perform element-by-element true division operations of a DataFrame with another object (such as DataFrame, Series, or scalar). passaxisThe parameter can specify the axis of the operation, throughlevelParameters can handle multi-level indexes, throughfill_valueParameters can be filled with missing values. This is very useful for data processing and calculations, especially when dealing with missing values ​​and multi-level indexes.

This is the introduction to this article about the specific use of pandas DataFrame truediv. For more related pandas DataFrame truediv content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!