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.0
or'index'
Indicates operation along the line,1
or'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
Assumptiondf2
There 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,df2
The first element in it isNaN
,usefill_value=1
back,df1
Corresponding elements in1
Divided by1
, the result is still1
。
Example 4: Handling the case of dividing by zero
ifdf2
There is a zero value in , the result will beinf
or-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,df2
The second element in it is0
,df1
Corresponding elements in5
Divided by0
,turn outinf
。
Example 5: Specify axis parameter
Suppose we have a DataFrame and a Series, which can be specified byaxis
Parameters 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.02DataFrame 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). passaxis
The parameter can specify the axis of the operation, throughlevel
Parameters can handle multi-level indexes, throughfill_value
Parameters 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!