Pandas div – Python Pandas DataFrame div() Function

Pandas div: 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 div() Function:

Pandas div: The div() function of the Pandas module returns the element-wise floating division of a dataframe and other. It’s similar to dataframe / other, but with the ability to provide a fill_value as one of the parameters to replace missing data.

Syntax:

DataFrame.div(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.

Pandas DataFrame div() Function in Python

Example1

Here, to divide the entire DataFrame by a scalar value, we used the div() 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 div() function to the dataframe by passing some random number to it and print the result.
  • Here it divides the entire dataframe by 5.
  • 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 div() function to the dataframe by passing some random number
# to it and print the result.
# Here it divides the entire dataframe by 5.
print("The result dataframe after dividing the entire dataframe by 5:")
print(data_frme.div(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 dividing the entire dataframe by 5:
        student_rollno  student_marks
virat              0.2           16.0
nick               0.4            7.0
jessy              0.6            5.0
sindhu             0.8           18.0

Example2

Here, the ‘other’ argument can be provided as a list to divide different columns by different scalar values

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 div() function to the dataframe by passing list as an argument to it which divides elements of the dataframe by corresponding list elements and print the result.
  • Here it divides the student_rollno column of the dataframe by 2 and student_marks column of the dataframe by 10.
  • 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 div() function to the dataframe by passing list as an argument to it 
# which divides elements of the dataframe by corresponding list elements and print the result.
# Here it divides the student_rollno column of the dataframe by 2
# and student_marks column of the dataframe by 10.
print("The dataframe after dividing student_rollno by 2, student_marks by 10:")
print(data_frme.div([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 dividing student_rollno by 2, student_marks by 10:
        student_rollno  student_marks
virat              0.5            8.0
nick               1.0            3.5
jessy              1.5            2.5
sindhu             2.0            9.0

Example3

Here, div() 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 div() function to the specified column of dataframe by passing some random number to it and print the result.
  • Here it divides the student_rollno column by 5
  • Apply div() function to the specified columns of dataframe which divides elements of the specified columns by corresponding list elements and print the result.
  • Here it divides the student_rollno column of the dataframe by 2 and student_marks column of the dataframe by 10.
  • 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 div() function to the specified column of dataframe by passing some random number
# to it and print the result.
# Here it divides the student_rollno column by 5
print("The dataframe after dividing the student_rollno column by 5:")
print(data_frme["student_rollno"].div(5))
print()

# Apply div() function to the specified columns of dataframe which divides elements
# of the specified columns by corresponding list elements and print the result.
# Here it divides the student_rollno column of the dataframe by 2.
# and student_marks column of the dataframe by 10.
print("The dataframe after dividing student_rollno by 2, student_marks by 10:")
print(data_frme[["student_rollno", "student_marks"]].div([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 dividing the student_rollno column by 5:
virat     0.2
nick      0.4
jessy     0.6
sindhu    0.8
Name: student_rollno, dtype: float64

The dataframe after dividing student_rollno by 2, student_marks by 10:
        student_rollno  student_marks
virat              0.5            8.0
nick               1.0            3.5
jessy              1.5            2.5
sindhu             2.0            9.0

Example4

In a DataFrame, the div() function can be used to obtain the floating division of two series/column elements.

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.
  • Divide the column student_marks by student_rollno using the div() function and store it as a new column in the dataframe.
  • Print the dataframe after adding a new column(student_marks / student_rollno).
  • 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()

# Divide the column student_marks by student_rollno using the div() 
# function and store it as a new column in the dataframe.
data_frme['student_marks / student_rollno'] = data_frme['student_marks'].div(data_frme['student_rollno'])
# Print the dataframe after adding a new column(student_marks / student_rollno)
print("The DataFrame after adding a new column(student_marks / student_rollno):")
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_marks / student_rollno):
        student_rollno  student_marks  student_marks / student_rollno
virat                1             80                       80.000000
nick                 2             35                       17.500000
jessy                3             25                        8.333333
sindhu               4             90                       22.500000