Skip to main content

The mul() method of DataFrame object multiplies the elements of a DataFrame object with another DataFrame object, series or any other Python sequence.

mul()  does an elementwise multiplication of a DataFrame with another DataFrame, a pandas Series or a  Python Sequence.

Calling the mul() method is similar to using the binary multiplication operator(*).

The mul() method provides a parameter fill_value using which values can be passed to replace the np.nan, None values present in the data.

The multiplication function of pandas is used to perform multiplication operations on dataframes.

Syntax

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

  • other : scalar, sequence, Series, or DataFrame – This parameter consists any single or multiple element data structure, or list-like object.
  • axis : {0 or ‘index’, 1 or ‘columns’} – This is used for deciding the axis on which the operation is applied.
  • level : int or label – The level parameter is used for broadcasting across a level and matching Index values on the passed MultiIndex level.
  • fill_value : float or None, default None – Whenever the dataframes have missing values, then to fill existing missing (NaN) values, we can use fill_value parameter.

Example 1: Simple example of pandas multiplication() function

input:

df1 = pd.DataFrame({'speed': [50, 75, 100]},
                    index=['Audi', 'Jaguar', 'BMW'])
df1

Output:

  speed
Audi 50
Jaguar 75
BMW 100

Input :

res = df.mul(df1)
df

Output:

  speed weight
Audi 80 250
Jaguar 90 200
BMW 110 150

Input :

res

Output:

  speed weight
Audi 4000 NaN
Jaguar 6750 NaN
BMW 11000 NaN

Example 2: Understanding use of fill_value parameter

Here the missing values of the new dataframe is filled with the help of fill_value parameter.

Input:

df

Output:

  speed weight
Audi 80 250
Jaguar 90 200
BMW 110 150

Input:

df.mul(df1, fill_value=0)

Output:

  speed weight
Audi 4000 0.0
Jaguar 6750 0.0
BMW 11000 0.0

Example 3: Perform multiplication operation using “*”

We can perform multiplication operation using the asterisk symbol as well. We can see that the output generated through this method and through mul() operation is same.

Input:

df

Output:

  speed weight
Audi 80 250
Jaguar 90 200
BMW 110 150

Input:

df1

Output:

  speed
Audi 50
Jaguar 75
BMW 100

Input:

df * df1

Output:

  speed weight
Audi 4000 NaN
Jaguar 6750 NaN
BMW 11000 NaN

Example 4:

input : 


import pandas as pd

 

# Create data

dataSet1 =   [(2, 0, 1, 0),

              (1, 0, 3, 0),

              (4, 3, 2, 0)];

 

dataSet2 =   [(1, 1, 2, 1),

              (1, 2, 1, 1),

              (3, 1, 1, 3)];

             

# Construct pandas DataFrame instances

dataFrame1 = pd.DataFrame(data=dataSet1);

dataFrame2 = pd.DataFrame(data=dataSet2);

 

print("Elements present in DataFrame1:");

print(dataFrame1);        

 

print("Elements present in DataFrame2:");

print(dataFrame2);

 

# Multiply two DataFrames

multiplicationResults = dataFrame1.mul(dataFrame2);

print("Result of element-wise multiplication of two Data Frames:");

print(multiplicationResults);

output:

Elements present in DataFrame1:

   0  1  2  3

0  2  0  1  0

1  1  0  3  0

2  4  3  2  0

Elements present in DataFrame2:

   0  1  2  3

0  1  1  2  1

1  1  2  1  1

2  3  1  1  3

Result of element-wise multiplication of two Data Frames:

    0  1  2  3

0   2  0  2  0

1   1  0  3  0

2  12  3  2  0
Tags
Submitted by shiksha.dahiya on February 16, 2021

Shiksha is working as a Data Scientist at iVagus. She has expertise in Data Science and Machine Learning.

About

Elix is a premium wordpress theme for portfolio, freelancer, design agencies and a wide range of other design institutions.