Python | Pandas Series.mul ()

Python Methods and Functions

Python Series.mul() is used to multiply series or lists of similar objects with the same length by a number of callers.

Syntax: Series.mul (other, level = None, fill_value = None, axis = 0)

Parameters:
other: other series or list type to be multiplied with caller series
fill_value: Value to be replaced by NaN in series / list before multiplication
level: integer value of level in case of multi index

Return type: Caller series with multiplied values ​​

To load a dataset, used in the following example, click here.
In the following examples used the data frame contains the data of some NBA players. An image of the data frame before any operations is attached below. 

Example # 1: Multiplying a list with series

In this example, the first 5 lines are stored in a new variable using the .head () method. Then a list of the same length is created and multiplied by the Age column using the .mul () method.

# pandas module import

import pandas as pd

 
# read CSV file from URL

data = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/nba.csv " )

 
# create short data of 5 lines

short_data = data.head ()

 
# create a list with 5 values ​​

list = [ 1 , 2 , 3 , 4 , 5 ]

  
# multiply the list of data
# create a new column

short_data [ "Multiplied values" ] = short_data [ "Age" ]. mul ( list )

 
# display
short_data

Output:
As shown in the output image, it can be compared that the Multiplied Values ​​column has Multiplied Values ​​(Age) x (list). 

Example # 2: Multiplying rows by rows with zero values

This example multiplies the Salary column by the Age column. Because the values ​​in the Salary and Age columns are large, the product will be returned with a high value. Hence, just for demonstration, the age column is divided by 100 before multiplying. Since the salary column also contains null values, by default it returns NaN no matter what is multiplied. This example passes 20 to replace null values ​​with 20.

# pandas module import

import pandas as pd

 
# read CSV file from URL

data = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/nba.csv " )

 
# age divisions

data [ "Age" ] = < code class = "plain"> data [ "Age" ] / 100

 

age = data [ " Age " ]

 
# to replace

na = 20

 
# Multiply values ​​
# save to a new column

data [ "Multiplied values" ] = data [ " Salary " ]. mul (other = age, fill_value = na)

 
# display
data

Quit:
As shown in the output In the image, in the Multiplied Value column, the age column is multiplied by 20 for NULL values. 





Get Solution for free from DataCamp guru