Python | Pandas dataframe.floordiv()
Last Updated :
01 Jun, 2021
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas dataframe.floordiv() function is used for integer division of the dataframe with either a constant, series or any other dataframe.
If other is a series then, the dimension of series must match with the division axis of the dataframe. If other is a data frame then, both the dataframes should have the same dimension.
Equivalent to dataframe/other, but with support to substitute a fill_value for missing data in one of the inputs.
Syntax: DataFrame.floordiv(other, axis=’columns’, level=None, fill_value=None)
Parameters:
other : Series, DataFrame, or constant
axis : For Series input, axis to match Series index on
fill_value : Fill missing (NaN) values with this value. If both DataFrame locations are missing, the result will be missing
level : Broadcast across a level, matching Index values on the passed MultiIndex level
Returns : result : DataFrame
Example #1: Use floordiv() function to find the integer division of a dataframe with a constant. Dataframe contains NA value.
Python3
import pandas as pd
df = pd.DataFrame({ "A" :[ 5 , 3 , 6 , 4 ],
"B" :[ 11 , None , 4 , 3 ],
"C" :[ 4 , 3 , 8 , None ],
"D" :[ 5 , 4 , 2 , 8 ]})
df
|
Now apply the floordiv() function. In our dataframe we are having NA values. We fill all such values with 50.
Python3
df.floordiv( 2 , fill_value = 50 )
|
Output :
Notice, all the non-Na value in the dataframe has been filled with 50 before performing the integer division.
Example #2: Use floordiv() function to find the integer division of a dataframe with a Series.
Python3
import pandas as pd
df = pd.DataFrame({ "A" :[ 5 , 3 , 6 , 4 ],
"B" :[ 11 , 2 , 4 , 3 ],
"C" :[ 4 , 3 , 8 , 5 ],
"D" :[ 5 , 4 , 2 , 8 ]})
sr = pd.Series([ 2 , 1 , 3 , 1 ])
df.floordiv(sr, axis = 0 )
|
Output :
Each row of the dataframe is divided by the corresponding value in the series object.
Share your thoughts in the comments
Please Login to comment...