SoFunction
Updated on 2025-04-23

The specific implementation of pandas DataFrame mul

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

()

()Methods are used to perform element-by-element multiplication operations. This method can be used to multiply between two DataFrames, or multiply between a 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.
  • level: If the index is a multi-index, you can specify which level to operate along.
  • fill_value: If a missing value (NaN) is encountered, you can use this value to fill it.

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: Multiplication between DataFrame and DataFrame

result = (df2)
print(result)

Output:

   A  B
0  1  8
1  2 10
2  3 12

Example 2: Multiplication between DataFrame and scalar

result = (2)
print(result)

Output:

   A  B
0  2  8
1  4 10
2  6 12

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  8
1  2.0 10
2  3.0 12

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

These examples show()Basic usage of the method and some common situations.

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