Pandas DataFrame | rfloordiv method
Start your free 7-days trial now!
Pandas DataFrame.rfloordiv(~)
method performs integer division between a scalar, sequence, Series or DataFrame and the values in the source DataFrame, that is:
other // DataFrame
Note that this is the reverse of DataFrame.floordiv(~)
, which does the following:
DataFrame // other
Unless you use the parameters axis
, level
and fill_value
, rfloordiv(~)
is equivalent to performing division using the //
operator.
Parameters
1. other
link | scalar
or sequence
or Series
or DataFrame
The resulting DataFrame will be other
divided by the source DataFrame via integer division.
2. axis
link | int
or string
| optional
Whether to broadcast other
for each column or row of the source DataFrame:
Axis | Description |
---|---|
|
|
|
|
Note that this is only relevant if the shape of the source DataFrame and that of other
does not match up. By default, axis=1
.
3. level
| int
or string
| optional
The name or the integer index of the level to consider. This is relevant only if your DataFrame is Multi-index.
4. fill_value
link | float
or None
| optional
The value to replace NaN
before the computation. Note that if both pair of entries are NaN
, then the result would still be NaN
. By default, fill_value=None
.
Return Value
A new DataFrame resulting from the integer division.
Examples
Basic usage
Consider the following DataFrame:
df = pd.DataFrame({"A":[1,2], "B":[3,4]})df_other = pd.DataFrame({"A":[5,6], "B":[7,8]})
A B | A B0 1 3 | 0 5 71 2 4 | 1 6 8
Performing integer division:
df.rfloordiv(df_other)
A B0 5 21 3 2
Here, we're performing the following element-wise integer division:
5//1 7//36//2 8//4
Note that this is equivalent to:
df_other // df
A B0 5 21 3 2
Broadcasting
Consider the following DataFrame:
df = pd.DataFrame({"A":[2,3], "B":[4,5]})df
A B0 2 41 3 5
Row-wise integer division
By default, axis=1
, which means that other
will be broadcasted for each row in df
:
df.rfloordiv([7,8]) # axis=10
A B0 3 21 2 1
Here, we're performing the following element-wise integer division:
7//2 8//47//3 8//5
Column-wise integer division
To broadcast other
for each column in df
, set axis=0
like so:
df.rfloordiv([7,8], axis=0)
A B0 3 11 2 1
Here, we're performing the following element-wise division:
7//2 7//48//3 8//5
Specifying fill_value
Consider the following DataFrames:
df = pd.DataFrame({"A":[2,np.NaN], "B":[np.NaN,5]})df_other = pd.DataFrame({"A":[7,8],"B":[np.NaN,np.NaN]})
A B | A B0 2.0 NaN | 0 7 NaN1 NaN 5.0 | 1 8 NaN
By default, when we compute the integer division using rfloordiv(~)
, any operation with NaN
results in NaN
:
df.rfloordiv(df_other)
A B0 3.0 NaN1 NaN NaN
We can fill NaN
s before we perform integer division by using the fill_value
parameter:
df.rfloordiv(df_other, fill_value=2)
A B0 3.0 NaN1 2.0 0.0
Notice how if the integer division is between two NaN
, then the result would always be NaN
regardless of fill_value
.