Articles

pandas mul() function | multiply pandas dataframe & column by constant

pandas mul() function | multiply pandas dataframe & column by constant


In this pandas tutorial, we will discuss about:

  • pandas mul function,
  • multiply pandas dataframe by constant,
  • multiply pandas column by constant,

Before going ahead with understanding pandas mul function, lets see what is dataframe.

 

DataFrame in pandas is an 2-dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.

 

We can able to create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.

Syntax:

pandas.DataFrame(data)

Where, data is the input dataframe. The data can be a dictionary that stores list of values with specified key.

 

Example: Create dataframe

In this example, we will create a dataframe with 4 rows and 3 columns with building data and assign indices through index parameter.

import pandas as pd

#create dataframe from the building data
data= pd.DataFrame({
                    'length':[5.6,7.8,4.5,5.3],

                   "breadth":[12.9,4.5,21.5,6.0],

                    "area":[20,56,43,45]

                   },index=['one','two','three','four'])

#display the dataframe
print(data)

Output: Dataframe is created below

       length  breadth  area
one       5.6     12.9    20
two       7.8      4.5    56
three     4.5     21.5    43
four      5.3      6.0    45

Now lets use this dataframe to understand method mul in pandas.


pandas mul function

We can multiply entire dataframe or to the particular column with a value in the dataframe using mul() method or pandas mul function.

Syntax:

dataframe.mul(value)
(or)
dataframe['column'].mul(value)

where,

1. dataframe is the input dataframe

2. column refers column name where value to be multiplied

3. value represents the numeric value to be multiplied with dataframe column/ entire dataframe.

 

Example 1multiply pandas dataframe by constant

In this multiply pandas dataframe by constant example, we will multipy entire dataframe with some values.

import pandas as pd

#create dataframe from the building data
data= pd.DataFrame({
                    'length':[5.6,7.8,4.5,5.3],

                   "breadth":[12.9,4.5,21.5,6.0],

                    "area":[20,56,43,45]

                   },index=['one','two','three','four'])

# multiply the dataframe with 4
print(data.mul(4))

print()

# multiply the dataframe with 20
print(data.mul(20))

print()

# multiply the dataframe with 0
print(data.mul(0))

Output: multiply pandas dataframe by constant 4, 20 and 0 result

       length  breadth  area
one      22.4     51.6    80
two      31.2     18.0   224
three    18.0     86.0   172
four     21.2     24.0   180

       length  breadth  area
one     112.0    258.0   400
two     156.0     90.0  1120
three    90.0    430.0   860
four    106.0    120.0   900

       length  breadth  area
one       0.0      0.0     0
two       0.0      0.0     0
three     0.0      0.0     0
four      0.0      0.0     0

We can also use '*' operator to multiply value to the dataframe.

Example: multiply pandas dataframe by constant using * operator

import pandas as pd

#create dataframe from the building data
data= pd.DataFrame({
                    'length':[5.6,7.8,4.5,5.3],

                   "breadth":[12.9,4.5,21.5,6.0],

                    "area":[20,56,43,45]

                   },index=['one','two','three','four'])

# multiply dataframe with 4
print(data*4)

print()

# multiply dataframe with 20
print(data*20)

print()

# multiply dataframe with 0
print(data*0)

Output: multiply pandas dataframe by constant result

       length  breadth  area
one      22.4     51.6    80
two      31.2     18.0   224
three    18.0     86.0   172
four     21.2     24.0   180

       length  breadth  area
one     112.0    258.0   400
two     156.0     90.0  1120
three    90.0    430.0   860
four    106.0    120.0   900

       length  breadth  area
one       0.0      0.0     0
two       0.0      0.0     0
three     0.0      0.0     0
four      0.0      0.0     0

Lets see another example where we multiply pandas column by constant.


Example 2multiply pandas column by constant

In this multiply pandas column by constant example, we will multiply some values to the particular column of the dataframe.

import pandas as pd

#create dataframe from the building data
data= pd.DataFrame({
                    'length':[5.6,7.8,4.5,5.3],

                   "breadth":[12.9,4.5,21.5,6.0],

                    "area":[20,56,43,45]

                   },index=['one','two','three','four'])

# multiply length column with 4
print(data['length'].mul(4))

print()

# multiply length column with 20
print(data['breadth'].mul(20))

Outputmultiply pandas column by constant result

one      22.4
two      31.2
three    18.0
four     21.2
Name: length, dtype: float64

one      258.0
two       90.0
three    430.0
four     120.0
Name: breadth, dtype: float64

We can also use *  operator to multiply values.

Examplemultiply pandas column by constant using * operator

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({
                    'length':[5.6,7.8,4.5,5.3],

                   "breadth":[12.9,4.5,21.5,6.0],

                    "area":[20,56,43,45]

                   },index=['one','two','three','four'])

# multiply  breadth column with 4
print(data['length']*4)

print()

# multiply  breadth column with 20
print(data['breadth']*20)

Outputmultiply pandas column by constant result

one      22.4
two      31.2
three    18.0
four     21.2
Name: length, dtype: float64

one      258.0
two       90.0
three    430.0
four     120.0
Name: breadth, dtype: float64

Thus we have seen how to multiply pandas column by constant and multiply pandas dataframe by constant using pandas mul function


Pandas

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 800+ Technical Articles on Python, R, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Apr 19,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!