Pandas Tutorial Pandas References

Pandas DataFrame - mul() function



The Pandas mul() function returns multiplication of dataframe and other, element-wise. It is equivalent to dataframe * other, but with support to substitute a fill_value for missing data as one of the parameters.

Syntax

DataFrame.mul(other, axis='columns', 
              level=None, fill_value=None)

Parameters

other Required. Specify any single or multiple element data structure, or list-like object.
axis Optional. Specify whether to compare by the index (0 or 'index') or columns (1 or 'columns'). For Series input, axis to match Series index on. Default is 'columns'.
level Optional. Specify int or label 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 DataFrame alignment. If data in both corresponding DataFrame locations is missing the result will be missing. Default is None.

Return Value

Returns the result of the arithmetic operation.

Example: using mul() on whole DataFrame

In the example below, a DataFrame df is created. The mul() function is used to multiply the whole DataFrame by a given scalar value.

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:")
print(df)

#multiplying all entries of the DataFrame by 2
print("\ndf.mul(2) returns:")
print(df.mul(2))

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

df.mul(2) returns:
       Bonus  Salary
John      10     120
Marry      6     124
Sam        4     130
Jo         8     118

Example: Multiplying different column by different value

Different column can be multiplied by different scalar value by providing other argument as a list. 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:")
print(df)

#multiplying all entries of Bonus column by 2
#multiplying all entries of Salary column by 10
print("\ndf.mul([2,10]) returns:")
print(df.mul([2,10]))

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

df.mul([2,10]) returns:
       Bonus  Salary
John      10     600
Marry      6     620
Sam        4     650
Jo         8     590

Example: using mul() on selected columns

Instead of whole DataFrame, the mul() function can be applied on selected columns. Consider the following example.

import pandas as pd
import numpy as np

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

print("The DataFrame is:")
print(df)

#multiplying all entries of Salary column by 3
print("\ndf['Salary'].mul(3) returns:")
print(df["Salary"].mul(3))

#multiplying all entries of Salary column by 3
#multiplying all entries of Bonus column by 2
print("\ndf[['Salary', 'Bonus']].mul([3,2]) returns:")
print(df[["Salary", "Bonus"]].mul([3,2]))

The output of the above code will be:

The DataFrame is:
       Bonus  Last Salary  Salary
John       5           58      60
Marry      3           60      62
Sam        2           63      65
Jo         4           57      59

df['Salary'].mul(3) returns:
John     180
Marry    186
Sam      195
Jo       177
Name: Salary, dtype: int64

df[['Salary', 'Bonus']].mul([3,2]) returns:
       Salary  Bonus
John      180     10
Marry     186      6
Sam       195      4
Jo        177      8

Example: Multiplying columns in a DataDrame

The mul() function can be applied in a DataFrame 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:")
print(df)

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

print("\nThe DataFrame is:")
print(df)

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 DataFrame - Functions