Pandas DataFrame.rfloordiv


Pandas DataFrame.rfloordiv

The DataFrame.rfloordiv method in pandas performs element-wise integer division (floor division) on a DataFrame, with the operation applied in reverse order. This means other // DataFrame, where other can be a scalar, a Series, or another DataFrame. The operation aligns on both row and column labels.


Syntax

The syntax for DataFrame.rfloordiv is:

DataFrame.rfloordiv(other, axis='columns', level=None, fill_value=None)

Here, DataFrame is the object being operated on, and other is the divisor.


Parameters

ParameterDescription
otherA scalar, Series, or DataFrame to divide the DataFrame by. This is the numerator in the reverse floor division.
axisSpecifies whether to align the operation along rows (0 or 'index') or columns (1 or 'columns'). Default is 'columns'.
levelIf the axis is a MultiIndex (hierarchical), this parameter specifies the level to align on. Default is None.
fill_valueSpecifies a value to fill in for missing data in either DataFrame or other. Default is None.

Returns

A DataFrame resulting from the reverse floor division operation, with aligned labels and filled values as specified.


Examples

Reverse Floor Division with a Scalar

Perform reverse floor division with a scalar value.

Python Program

import pandas as pd

# Create a DataFrame
data = {
    'A': [1, 2, 3],
    'B': [4, 5, 6]
}
df = pd.DataFrame(data)

# Perform reverse floor division with a scalar
print("Reverse Floor Division with a Scalar:")
result = df.rfloordiv(10)
print(result)

Output

Reverse Floor Division with a Scalar:
    A  B
0  10  2
1   5  2
2   3  1

Reverse Floor Division with Another DataFrame

Perform reverse floor division with another DataFrame, aligning labels.

Python Program

import pandas as pd

# Create two DataFrames
data1 = {
    'A': [2, 4, 6],
    'B': [8, 10, 12]
}
data2 = {
    'A': [1, 1, 1],
    'B': [3, 3, 3]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)

# Perform reverse floor division
print("Reverse Floor Division with Another DataFrame:")
result = df2.rfloordiv(df1)
print(result)

Output

Reverse Floor Division with Another DataFrame:
    A  B
0    2  2
1    4  3
2    6  4

Handling Missing Values with fill_value

Use the fill_value parameter to handle missing data in either DataFrame.

Python Program

import pandas as pd
import numpy as np

# Create two DataFrames with missing values
data1 = {
    'A': [2, np.nan, 6],
    'B': [8, 10, 12]
}
data2 = {
    'A': [1, 1, 1],
    'B': [3, np.nan, 3]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)

# Perform reverse floor division with a fill_value
print("Reverse Floor Division with fill_value=1:")
result = df2.rfloordiv(df1, fill_value=1)
print(result)

Output

Reverse Floor Division with fill_value=1:
     A    B
0  2.0  2.0
1  1.0  1.0
2  6.0  4.0

Using axis Parameter

Control the alignment of the operation using the axis parameter.

Python Program

import pandas as pd

# Create a DataFrame and a Series
data = {
    'A': [2, 4, 6],
    'B': [8, 10, 12]
}
df = pd.DataFrame(data)
series = pd.Series([2, 4, 6], index=[0, 1, 2])

# Perform reverse floor division along the index (axis=0)
print("Reverse Floor Division with axis=0:")
result = df.rfloordiv(series, axis=0)
print(result)

Output

Reverse Floor Division with axis=0:
    A  B
0    1  4
1    1  2
2    1  2

Summary

In this tutorial, we explored the DataFrame.rfloordiv method in pandas. Key takeaways include:

  • Using reverse floor division with scalars, Series, or DataFrames.
  • Handling missing values with the fill_value parameter.
  • Customizing alignment with the axis parameter.

The DataFrame.rfloordiv method is a useful tool for reverse integer division, especially in complex alignment scenarios.


Python Libraries