Python | Pandas Series.divide ()

Python Methods and Functions

Series.divide() Pandas Series.divide() performs floating division of rows and others, element by element (binary truediv operator) This is equivalent to series / other , but with support for fill_value replacement for missing data in one of the inputs.

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

Parameter:
other: Series or scalar value
fill_value: Fill existing missing (NaN) values.
level: Broadcast across a level, matching Index values ​​on the passed MultiIndex level

Returns: result: Series

Example # 1: Use Series.divide () to perform floating division on a given series object with a scalar.

# import pandas as pd

import pandas as pd

 
# Create a series

sr = pd.Series ([ 80 , 25 , 3 , 25 , 24 , 6 ])

 
# Create Index

index_ = [ `Coca Cola` , ` Sprite` , `Coke` , `Fanta` , ` Dew` , `ThumbsUp` ]

  
# set index

sr.index = index_

 
# Print series

print (sr)

Output:

We will now use Series.divide () to perform floating division on a given series object with a scalar.

# do floating division

result = sr.divide (other = 2 )

 
# Print result

print (result)

Output:

As we can see in the output, Series.divide () has successfully floated division on this scalar series object.

Example # 2: Use Series.divide () to float on this scalar series object ... This series object contains some missing values.

# import pandas as pd

import pandas as pd

 
# Create series

sr = pd.Series ([ 100 , None , None , 18 , 65 , None , 32 , 10 , 5 , 24 , None ])

 
# Create index

index_ = pd.date_range ( ` 2010-10-09` , periods = 11 , freq = `M` )

  
# set index

sr.index = index_

 
# Print series

print (sr)

Exit:

We will now use Series.divide ( ) to perform floating division on a given series object with a scalar. We`re going to fill 50 in place of any missing values.

# perform floating division
# fill in 50 instead of missing values ​​

result = sr.divide (other = 2 , fill_value = 50 )

  
# Print result

print (result)

Exit :

As we can see in the output, Series .divide () successfully floated division of this series object with a scalar.





Get Solution for free from DataCamp guru