Pandas Tutorial Pandas Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

Pandas Series - mul() function

The Pandas mul() function is used to get multiplication of series and argument, element-wise (binary operator mul). It is equivalent to series * other, but with support to substitute a fill_value for missing data as one of the parameters. The syntax for using this function is given below:


Series.mul(other, level=None, fill_value=None)


other Required. Specify scalar value or Series.
level Optional. Specify int or name to broadcast across a level, matching Index values on the passed MultiIndex level. Default is None.
fill_value Optional. Specify value to fill existing missing (NaN) values, and any new element needed for successful Series alignment. If data in both corresponding Series locations is missing the result will be missing. Default is None.

Return Value

Returns the result of the arithmetic operation.

Example: Multiplying th Series with a scalar value

In the example below, the mul() function is used to multiply the given series with a scalar value.

import pandas as pd
import numpy as np

x = pd.Series([10, 20, 30, 40, 50])

print("The Series contains:")

#multiplying the Series with 3
print("\nx.mul(3) returns:")

The output of the above code will be:

The Series contains:
0    10
1    20
2    30
3    40
4    50
dtype: int64

x.mul(3) returns:
0     30
1     60
2     90
3    120
4    150
dtype: int64

Example: Multiplying two Series

A series can be multiplied with another series in the similar fashion. Consider the following example:

import pandas as pd
import numpy as np

x = pd.Series([10, np.NaN, 30, 40], index=['A', 'B', 'C', 'D'])
y = pd.Series([1, 2, 3, np.NaN], index=['A', 'B', 'C', 'D'])

print("The x contains:")

print("\nThe y contains:")

#multiplying two Series
print("\nx.mul(y, fill_value=0) returns:")
print(x.mul(y, fill_value=0))

The output of the above code will be:

The x contains:
A    10.0
B     NaN
C    30.0
D    40.0
dtype: float64

The y contains:
A    1.0
B    2.0
C    3.0
D    NaN
dtype: float64

x.mul(y, fill_value=0) returns:
A    10.0
B     0.0
C    90.0
D     0.0
dtype: float64

Example: Multiplying columns in a DataDrame

The mul() function can be applied in a data frame to get the multiplication of two series/column element-wise. Consider the following example.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "%Bonus": [5, 3, 2, 4],
  "Salary": [60, 62, 65, 59]},
  index= ["John", "Marry", "Sam", "Jo"]

print("The DataFrame is:")

#multiplying '%Bonus' with 'Salary' column
df['Bonus'] = df['Salary'].mul(df['%Bonus']/100)

print("\nThe DataFrame is:")

The output of the above code will be:

The DataFrame is:
       %Bonus  Salary
John        5      60
Marry       3      62
Sam         2      65
Jo          4      59

The DataFrame is:
       %Bonus  Salary  Bonus
John        5      60   3.00
Marry       3      62   1.86
Sam         2      65   1.30
Jo          4      59   2.36

❮ Pandas Series - Functions