Skip to main content

div() method divides element-wise division of one pandas DataFrame by another. DataFrame elements can be divided by a pandas series or by a Python sequence as well.

Calling div() on a DataFrame instance is equivalent to invoking the division operator (/). The div() method provides the fill_value parameter which is used for replacing the np.nan and None values present in the DataFrame or in the resultant value with any other value.

The division function of pandas is used to perform division operation on dataframes.

Syntax

pandas.DataFrame.div(other, axis=’columns’, level=None, fill_value=None)

  • other : scalar, sequence, Series, or DataFrame – This parameter consists any single or multiple element data structure, or list-like object.
  • axis : {0 or ‘index’, 1 or ‘columns’} – This is used for deciding the axis on which the operation is applied.
  • level : int or label – The level parameter is used for broadcasting across a level and matching Index values on the passed MultiIndex level.
  • fill_value : float or None, default None – Whenever the dataframes have missing values, then to fill existing missing (NaN) values, we can use fill_value parameter.

Example 1: Using pandas div() function

To learn more about the div() function in pandas, we will look at this example where div() function is used to perform division operation over dataframes.

Input:

df

Output:

  speed weight
Audi 80 250
Jaguar 90 200
BMW 110 150

Input:

df.div(10)

Output:

  speed weight
Audi 8.0 25.0
Jaguar 9.0 20.0
BMW 11.0 15.0

Example 2: Using div() function on multindex dataframe

In this example, a multindex dataframe is created and then division function is passed this multindex dataframe.

Input:

df_multindex = pd.DataFrame({'no_gears': [4, 5, 3, 3, 8, 6],
                             'Speed': [360, 180, 360, 360, 540, 720]},
                              index=[['Sedan', 'Sedan', 'Sedan', 'Hatchback',
                                              'Hatchback', 'Hatchback'],
                                     ['BMW', 'Audi', 'Bentley',
                                    'Mercedes', 'Jaguar', 'Mini Cooper']])

Input:

df_multindex

Output:

    no_gears Speed
Sedan BMW 4 360
Audi 5 180
Bentley 3 360
Hatchback Mercedes 3 360
Jaguar 8 540
Mini Cooper 6 720

Input:

df_multindex

Output:

  speed weight
Audi 80 250
Jaguar 90 200
BMW 110 150

Input:

df.div(df_multindex, level=1, fill_value=2)

Output:

    Speed no_gears speed weight
Sedan BMW 0.005556 0.500000 55.0 75.0
Audi 0.011111 0.400000 40.0 125.0
Bentley 0.005556 0.666667 NaN NaN
Hatchback Mercedes 0.005556 0.666667 NaN NaN
Jaguar 0.003704 0.250000 45.0 100.0
Mini Cooper 0.002778 0.333333 NaN NaN

Example:

input:

import pandas as pd

 

# Data creation part

data1 = [(20, 40, 60, 80),

         (10, 30, 50, 70),

         (15, 25, 35, 45)];

 

data2 = [(2, 3, 4, 6),

         (5, 1, 0, 3),

         (3, 6, 9, 1)];

 

# Creation of pandas DataFrames

dataFrame1 = pd.DataFrame(data=data1);

dataFrame2 = pd.DataFrame(data=data2);

 

# Divide the DataFrame1 elements by the elements of DataFrame2

divisionResults = dataFrame1.div(dataFrame2);

 

print("Elements of DataFrame1:")

print(dataFrame1);

 

print("Elements of DataFrame2:")

print(dataFrame2);

 

print("DataFrame1 elements divided by DataFrame2 elements:")

print(divisionResults);

output:

Output of a python example program that divides one DataFrame by another DataFrame

Tags
Submitted by shiksha.dahiya on February 16, 2021

Shiksha is working as a Data Scientist at iVagus. She has expertise in Data Science and Machine Learning.

About

Elix is a premium wordpress theme for portfolio, freelancer, design agencies and a wide range of other design institutions.