Signup/Sign In

Pandas DataFrame floordiv() Method

In this tutorial, we will learn the Python pandas DataFrame.floordiv() method. This method is used to get Integer division of dataframe and other, element-wise (binary operator floordiv). It is equivalent to dataframe // other, but with support to substitute a fill_value for missing data in one of the inputs. With reverse version, rfloordiv method can be used.

The below shows the syntax of the DataFrame.floordiv() method.

Syntax

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

Parameters

other: scalar, sequence, Series, or DataFrame. Any single or multiple element data structure, or list-like object.

axis:{0 or ‘index’, 1 or ‘columns’}.Whether to compare by the index (0 or ‘index’) or columns (1 or ‘columns’). For Series input, axis to match Series index on.

level: int or label. Broadcast across a level, matching Index values on the passed MultiIndex level.

fill_value: float or None, default None. Fill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing.

Example 1: Getting the integer division by using the DataFrame.floordiv() Method

Let's apply the DataFrame.floordiv() method to the DataFrame and get the integer division with constant. The below example shows the running code.

#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
#creating the DataFrame
df=pd.DataFrame({"A":[2,5],"B":[6,7],"C":[3,1]})
print("--------The DataFrame is---------")
print(df)
print("----After applying floordiv function-----")
print(df.floordiv(2))

Once we run the program we will get the following output.


--------The DataFrame is---------
A B C
0 2 6 3
1 5 7 1
----After applying floordiv method
A B C
0 1 3 1
1 2 3 0

Example 2: Getting the integer division by using the DataFrame.floordiv() Method

Let's apply the DataFrame.floordiv() method to the DataFrame and get the integer division with Series. The below example shows the runnig code.

#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
#creating the DataFrame
df=pd.DataFrame({"A":[9,5],"B":[3,7],"C":[3,1]})
print("--------The DataFrame is---------")
print(df)
series = pd.Series([2, 3]) 
print("----After applying floordiv function-----")
print(df.floordiv(series,axis=0) )

Once we run the program we will get the following output.


--------The DataFrame is---------
A B C
0 9 3 3
1 5 7 1
----After applying floordiv method-----
A B C
0 4 1 1
1 1 2 0

Example 3: Getting the integer division by using the DataFrame.floordiv() Method

Let's apply the DataFrame.floordiv() method to the DataFrame and get the integer division with fillvalue=30. The below example shows the running code.

#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
#creating the DataFrame
df=pd.DataFrame({"A":[2,5,None],"B":[6,7,np.nan],"C":[np.nan,3,1]})
print("--------The DataFrame is---------")
print(df)
print("----After applying floordiv function-----")
print(df.floordiv(3,fill_value = 30))

Once we run the program we will get the following output.


--------The DataFrame is---------
A B C
0 2.0 6.0 NaN
1 5.0 7.0 3.0
2 NaN NaN 1.0
----After applying floordiv method-----
A B C
0 0.0 2.0 10.0
1 1.0 2.0 1.0
2 10.0 10.0 0.0

Conclusion

In this tutorial, we learned the Python pandas DataFrame.floordiv() method. We learned the syntax, parameters and by applying this method on the DataFrame to understand the DataFrame.floordiv() method.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.

Want to learn coding and don't know where to start?

Try out our Interactive Courses for Free 🥳 😯 🤩
learn to code footer Ad