Skip to main content

Pandas DataFrame Multiplication: mul() function

Pandas dataframe.mul() function it is used to retrun the multiplication of dataframe and other element-wise. This function is equivalent to the DataFrame *  but it can provide an additional support to the handle the missing values in one of input. It returns a DataFrame with the result of the multiplication operation.

Syntax

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

Parameter:

  • other: It is a single or multiple data-structure or list-like object. It is a series sequence or dataframe.
  • axis: It is used to represent the index which is '0' for index and '1' for the column, and in series input, axis to match Series index on.
  • level: By Default None. It is used specify int or label to broadcast across a level, matching Index values on the passed MultiIndex level.
  • fill_value:  By Default None.  It is used to fill missing values or any new elements for successful alignment of dataframe with this value before computation. If data in both the dataframe with the same location is missing then the result is also missing.

Return:

It returns dataframe which is result of Arithmetic operation.

Examples

using mul()  function on whole DataFrame

import numpy as np
import pandas as pd

a = [11,21,35,46,57]
b = [10,30,50,40,60]
c = [33,45,67,76,55]

values = pd.DataFrame({'a':a,'b':b,'c':c})
print(values.mul(2))

Output:

     a    b    c
0   22   20   66
1   42   60   90
2   70  100  134
3   92   80  152
4  114  120  110

When one DataFrame contains null values

x = [5,5,6,7,8]
y = [3,5,6,7,8]
z = [np.NaN,np.NaN,np.NaN,np.NaN,np.NaN]

values = pd.DataFrame({'x':x,'y':y,'z':z})
print(values.mul(20,fill_value=10))

Output:

     x    y      z
0  100   60  200.0
1  100  100  200.0
2  120  120  200.0
3  140  140  200.0
4  160  160  200.0
Submitted by devanshi.srivastava on February 16, 2021

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.