Python is an excellent language for data analysis, due to a strong ecosystem of data-centric Python tools. One of these packages is Pandas, which makes importing and analyzing data a lot easier.
Pandas DataFrame mul() Function:
The mul() method of the Pandas module returns element-by-element multiplication of dataframe and other. It’s the same as dataframe * other, but with the ability to provide a fill_value as one of the parameters to replace missing data.
Syntax:
DataFrame.mul(other, axis='columns', level=None, fill_value=None)
Parameters
other: This is required. It indicates any single or multiple element data structure or list-like object. The type of this may be scalar, sequence, Series, or DataFrame
axis: This is optional. It indicates whether to compare by index (0 or ‘index’) and columns (1 or ‘columns’). axis to match Series index on, for Series input. The default value is ‘columns.’
level: This is optional. To broadcast over a level, specify an int or a label that matches the Index values on the passed MultiIndex level. None is the default value. The type of this may be int or label.
fill_value: This is optional. It indicates a value to fill in any current missing (NaN) values, as well as any new elements required for DataFrame alignment to succeed. The result will be missing if data in both corresponding DataFrame locations is missing. The type of this may be float or None. None is the default value.
Return Value:
The result of the arithmetic operation is returned.
- Python Pandas Series ge() Function
- Python Pandas Series ne() Function
- Python Pandas Series eq() Function
Pandas DataFrame mul() Function in Python
Example1
Here, to multiply a scalar value to the entire DataFrame, we used the mul() function.
Approach:
- Import pandas module using the import keyword.
- Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe
- Store it in a variable.
- Print the given dataframe
- Apply mul() function to the dataframe by passing some random number to it and print the result.
- Here it multiplies 5 with the entire dataframe.
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary), index list as arguments to the
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
"student_rollno": [1, 2, 3, 4],
"student_marks": [80, 35, 25, 90]},
index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()
# Apply mul() function to the dataframe by passing some random number
# to it and print the result.
# Here it multiplies 5 with the entire dataframe.
print("The result dataframe after multiplying 5 with the entire dataframe:")
print(data_frme.mul(5))
Output:
The given Dataframe:
student_rollno student_marks
virat 1 80
nick 2 35
jessy 3 25
sindhu 4 90
The result dataframe after multiplying 5 with the entire dataframe:
student_rollno student_marks
virat 5 400
nick 10 175
jessy 15 125
sindhu 20 450
Example2
Here, the ‘other’ argument can be provided as a list to multiply different scalar values with different columns.
Approach:
- Import pandas module using the import keyword.
- Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe
- Store it in a variable.
- Print the given dataframe.
- Apply mul() function to the dataframe by passing list as argument to it which multiplies corresponding list elements with the dataframe and print the result.
- Here it multiplies 2 with the student_rollno column of the dataframe and 10 with the student_marks column of the dataframe.
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary), index list as arguments to the
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
"student_rollno": [1, 2, 3, 4],
"student_marks": [80, 35, 25, 90]},
index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()
# Apply mul() function to the dataframe by passing list as argument to it
# which multiplies corresponding list elements with the dataframe and print the result.
# Here it multiplies 2 with the student_rollno column of the dataframe.
# and 10 with the student_marks column of the dataframe.
print("The dataframe after multiplying 2 with student_rollno, 10 with student_marks columns:")
print(data_frme.mul([2, 10]))
Output:
The given Dataframe:
student_rollno student_marks
virat 1 80
nick 2 35
jessy 3 25
sindhu 4 90
The dataframe after multiplying 2 with student_rollno, 10 with student_marks columns:
student_rollno student_marks
virat 2 800
nick 4 350
jessy 6 250
sindhu 8 900
Example3
Here, mul() function is used on specific columns rather than the entire DataFrame.
Approach:
- Import pandas module using the import keyword.
- Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe
- Store it in a variable.
- Print the given dataframe
- Apply mul() function to the specified column of dataframe by passing some random number to it and print the result.
- Here it multiplies 5 with the student_rollno column of the dataframe.
- Apply mul() function to the specified columns of dataframe which multiplies corresponding list elements with the dataframe columns and print the result.
- Here it multiplies 2 with the student_rollno column of the dataframe and 10 with the student_marks column of the dataframe.
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary), index list as arguments to the
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
"student_rollno": [1, 2, 3, 4],
"student_marks": [80, 35, 25, 90]},
index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()
# Apply mul() function to the specified column of dataframe by passing some random number
# to it and print the result.
# Here it multiplies 5 with the student_rollno column of the dataframe.
print("The dataframe after multiplying 5 with the student_rollno column:")
print(data_frme["student_rollno"].mul(5))
print()
# Apply mul() function to the specified columns of dataframe which multiplies corresponding
# list elements with the dataframe columns and print the result.
# Here it multiplies 2 with the student_rollno column of the dataframe
# and 10 with the student_marks column of the dataframe.
print("The dataframe after multiplying 2 with student_rollno, 10 with student_marks columns:")
print(data_frme[["student_rollno", "student_marks"]].mul([2, 10]))
Output:
The given Dataframe:
student_rollno student_marks
virat 1 80
nick 2 35
jessy 3 25
sindhu 4 90
The dataframe after multiplying 5 with the student_rollno column:
virat 5
nick 10
jessy 15
sindhu 20
Name: student_rollno, dtype: int64
The dataframe after multiplying 2 with student_rollno, 10 with student_marks columns:
student_rollno student_marks
virat 2 800
nick 4 350
jessy 6 250
sindhu 8 900
Example4
In a DataFrame, the mul() method can be used to obtain the element-wise multiplication of two series/columns.
Approach:
- Import pandas module using the import keyword.
- Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe
- Store it in a variable.
- Print the given dataframe.
- Multiply the columns student_rollno and student_marks using the mul() function and store it as a new column in the dataframe.
- Print the dataframe after adding a new column(student_rollno * student_marks).
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary), index list as arguments to the
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
"student_rollno": [1, 2, 3, 4],
"student_marks": [80, 35, 25, 90]},
index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()
# Multiply the columns student_rollno and student_marks using the mul()
# function and store it in as a new column in the dataframe.
data_frme['student_rollno * student_marks'] = data_frme['student_rollno'].mul(data_frme['student_marks'])
# Print the dataframe after adding a new column(student_rollno * student_marks)
print("The DataFrame after adding a new column(student_rollno * student_marks):")
print(data_frme)
Output:
The given Dataframe:
student_rollno student_marks
virat 1 80
nick 2 35
jessy 3 25
sindhu 4 90
The DataFrame after adding a new column(student_rollno * student_marks):
student_rollno student_marks student_rollno * student_marks
virat 1 80 80
nick 2 35 70
jessy 3 25 75
sindhu 4 90 360