Pandas multiply()

The multiply() method in Pandas is used to multiply elements within a DataFrame, with other DataFrame objects, or with scalar values.

Example

import pandas as pd

# create a DataFrame
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})

# create a second DataFrame
df2 = pd.DataFrame({
    'A': [10, 20, 30],
    'B': [40, 50, 60]
})

# multiply the two DataFrames using multiply() result = df.multiply(df2)
print(result) ''' Output A B 0 10 160 1 40 250 2 90 360 '''

multiply() Syntax

The syntax of the multiply() method in Pandas is:

df.multiply(other, axis='columns', fill_value=None)

multiply() Arguments

The multiply() method takes following arguments:

  • other - the value to multiply with the DataFrame. This could be scalar, another DataFrame, or a Series.
  • axis (optional) - When axis is 0, the Series aligns column-wise, and when axis is 1, it aligns row-wise..
  • fill_value (optional) - specifies a value to substitute for any missing values in the DataFrame or in the other object before multiplication

multiply() Return Value

The multiply() method returns a new object of the same type as the caller, which is either a DataFrame or a Series, depending on what is being multiplied.


Example 1: Multiply Two DataFrames

import pandas as pd

# define the first DataFrame
df1 = pd.DataFrame({
    'A': [1, 2],
    'B': [3, 4]
})

# define the second DataFrame
df2 = pd.DataFrame({
    'A': [5, 6],
    'B': [7, 8]
})

# perform element-wise multiplication result = df1.multiply(df2)
print(result)

Output

    A   B
0   5  21
1  12  32

In this example, df1.multiply(df2) multiplies each element of the df1 DataFrame with the corresponding element of the df2 DataFrame.


Example 2: Multiply DataFrame With a Scalar

import pandas as pd

# create a DataFrame
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})

# scalar to multiply with
scalar_value = 10

# multiply each element in df by scalar value df_multiplied = df.multiply(scalar_value)
print(df_multiplied)

Output

     A   B
0  10  40
1  20  50
2  30  60

Here, we multiplied each element in the df DataFrame by the scalar value of 10.


Example 3: Multiplication of DataFrame with Series

import pandas as pd

# create a DataFrame
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6],
    'C': [7, 8, 9]
})

# create two Series
series_for_columns = pd.Series([10, 100, 1000])
series_for_rows = pd.Series([10, 100, 1000], index=['A', 'B', 'C'])

# multiply along the columns (axis=0) result_columns = df.multiply(series_for_columns, axis=0)
print("Multiplication along columns:") print(result_columns) print("\n")
# multiply along the rows (axis=1) result_rows = df.multiply(series_for_rows, axis=1)
print("Multiplication along rows:") print(result_rows)

Output

Multiplication along columns:
      A     B     C
0    10    40    70
1   200   500   800
2  3000  6000  9000


Multiplication along rows:
     A    B     C
0   10  400  7000
1   20  500  8000
2   30  600  9000

Here,

  1. axis=0 - multiplies each column by the Series' values, matching by row index.
  2. axis=1 - multiplies each row by the Series' values, matching by column label.

Example 4: Multiply With fill_value Argument for Missing Data

import pandas as pd

# create a DataFrame with some missing values
df1 = pd.DataFrame({
    'A': [1, 2, None],
    'B': [4, None, 6]
})

# create another DataFrame with some missing values
df2 = pd.DataFrame({
    'A': [0.5, 2, 3],
    'B': [1, 0, None]
})

# use multiply() with fill_value
result = df1.multiply(df2, fill_value=1)

print(result)

Output

     A    B
0  0.5  4.0
1  4.0  0.0
2  3.0  6.0

In the above example, we have created two DataFrames: df1 and df2 with some missing values. The multiply() performs an element-wise multiplication of these DataFrames.

By specifying fill_value=1, Pandas treats any missing values in either DataFrame as 1 for the purpose of the multiplication.