Pandas DataFrame Rdiv Method

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will learn how to use the rdiv() method in pandas DataFrame. The rdiv() method is used to perform element-wise division in a DataFrame with other data structures, such as scalar, sequence, Series, or another DataFrame. It returns a new DataFrame with the result of the division operation.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/lists -.-> lab-68711{{"`Pandas DataFrame Rdiv Method`"}} python/tuples -.-> lab-68711{{"`Pandas DataFrame Rdiv Method`"}} python/dictionaries -.-> lab-68711{{"`Pandas DataFrame Rdiv Method`"}} python/importing_modules -.-> lab-68711{{"`Pandas DataFrame Rdiv Method`"}} python/numerical_computing -.-> lab-68711{{"`Pandas DataFrame Rdiv Method`"}} python/data_analysis -.-> lab-68711{{"`Pandas DataFrame Rdiv Method`"}} python/build_in_functions -.-> lab-68711{{"`Pandas DataFrame Rdiv Method`"}} end

Import the pandas library

import pandas as pd

Create a DataFrame

Let's start by creating a DataFrame using the pd.DataFrame() function. We will pass a dictionary containing column names as keys and lists as values.

df = pd.DataFrame({'a': [1, 6, 2], 'b': [3, 4, 6], 'c': [12, 1, 0]})
print("--------The DataFrame is----------")
print(df)

Perform division using rdiv() method

Now, let's perform division on the DataFrame using the rdiv() method. We will divide the DataFrame by a scalar value (12) and print the result.

print("---------------------------------")
print(df.rdiv(12))

Divide DataFrame with another DataFrame

Next, let's divide one DataFrame (df2) by another DataFrame (df1) using the rdiv() method. We will create two DataFrames and perform division using the rdiv() method.

df1 = pd.DataFrame({'a': [2, 2, 2], 'b': [2, 2, 2], 'c': [2, 2, 2]})
df2 = pd.DataFrame({'a': [2, 5, 6], 'b': [8, 10, 12], 'c': [14, 16, 18]})
print("---------------------------------")
print(df1.rdiv(df2))

Fill null values with a specified value

In some cases, the two DataFrames may not be aligned properly, resulting in NaN values after division. We can use the fill_value parameter of the rdiv() method to replace those NaN values with a specified value.

df1 = pd.DataFrame({'a': [2, 5, 6], 'b': [8, 10, 12]})
df2 = pd.DataFrame({'a': [2, 2, 2], 'b': [2, 2, 2], 'c': [2, 2, 2]})
print(df1.rdiv(df2, fill_value=2))

Summary

In this lab, we learned how to use the rdiv() method in pandas DataFrame to perform element-wise division. We learned how to divide a DataFrame by a scalar value, divide one DataFrame by another DataFrame, and fill null values with a specified value. The rdiv() method is a powerful tool for performing arithmetic operations on DataFrames in pandas.

Other Python Tutorials you may like