Pandas DataFrame multiply() Method
In this tutorial, we will discuss and learn the Python pandas DataFrame.multiply()
method. This method is used to get the multiplication of the dataframe and other, element-wise. It returns a DataFrame with the result of the multiplication operation.
The syntax is shown below.
Syntax
DataFrame.multiply(other, axis='columns', level=None, fill_value=None)
Parameters
other: It can be a scalar, sequence, Series, or DataFrame. It can be a single or multiple element data structure, or list-like object.
axis: It represents index or column axis, '0' for index and '1' for the column. When the axis=0
, method applied over the index
axis and when the axis=1
method applied over the column
axis. For the input Series
, axis to match Series index on.
level: It represents an int or label. It broadcasts across a level, matching Index values on the passed MultiIndex level.
fill_value: It represents the float or None, the default value is None. It fills the existing missing or null values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing.
Example 1: Multiply the DataFrame in Pandas
Here, we are multiplying the DataFrame with a scalar
value using the DataFrame.multiply()
method that returns a DataFrame that consists of the output of the multiplication operation. See the example below.
#importing pandas as pd
import pandas as pd
#creating DataFrame
df1 = pd.DataFrame({'a': [1,1,1],'b': [2,2,2],'c': [3,3,3]})
print(df1.multiply(2))
a b c
0 2 4 6
1 2 4 6
2 2 4 6
Example 2: Multiply the DataFrame in Pandas
Here, we are multiplying the DataFrame with the other DataFrame
using the DataFrame.multiply()
method that returns a DataFrame. It consists of the output of the multiplication operation. See the example below.
#importing pandas as pd
import pandas as pd
#creating DataFrame
df1 = pd.DataFrame({'a': [1,5,6],'b': [4,6,5],'c': [2,8,7]})
df2 = pd.DataFrame({'a': [2,1,1],'b': [1,5,8],'c': [7,5,6]})
print("------------The result is-----------")
print(df1.multiply(df2))
------------The result is-----------
a b c
0 2 4 14
1 5 30 40
2 6 40 42
Example 3: Multiply the DataFrame in Pandas
This example is similar to the previous one. If the two DataFrames are not aligned then the resultant output consisting of the NaN values. See the below example.
#importing pandas as pd
import pandas as pd
#creating DataFrame
df1 = pd.DataFrame({'a': [1,5,6],'b': [4,6,5],'c': [2,8,7]})
df2 = pd.DataFrame({'a': [2,1,1],'b': [1,5,8]})
print("------------The result is-----------")
print(df1.multiply(df2))
------------The result is-----------
a b c
0 2 4 NaN
1 5 30 NaN
2 6 40 NaN
Example 4: Multiply the DataFrame in Pandas
Here, in this example, we will fill any missing values by passing the fill_value=1
parameter in the DataFrame.multiply()
method. It will replace all the null values with the provided value. See the below example.
#importing pandas as pd
import pandas as pd
#creating DataFrame
df1 = pd.DataFrame({'a': [None,5,6],'b': [4,6,5],'c': [2,8,7]})
df2 = pd.DataFrame({'a': [None,1,1],'b': [None,5,8]})
print("------------The result is-----------")
print(df1.multiply(df2,fill_value=1))
------------The result is-----------
a b c
0 NaN 4.0 2.0
1 5.0 30.0 8.0
2 6.0 40.0 7.0
Conclusion
In this tutorial, we learned the Python pandas DataFrame.multiply()
method. We learned syntax, parameters of this method and applied it to the DataFrame.