Pandas Tutorial Pandas Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

Pandas DataFrame - floordiv() function



The Pandas floordiv() function is used to get integer division of dataframe and argument, element-wise (binary operator floordiv). It is equivalent to dataframe // other, but with support to substitute a fill_value for missing data as one of the parameters. The syntax for using this function is given below:

Syntax

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

Parameters

other Required. Specify any single or multiple element data structure, or list-like object.
axis Optional. Specify whether to compare by the index (0 or 'index') or columns (1 or 'columns'). For Series input, axis to match Series index on. Default is 'columns'.
level Optional. Specify int or label to broadcast across a level, matching Index values on the passed MultiIndex level. Default is None.
fill_value Optional. Specify value to fill existing missing (NaN) values, and any new element needed for successful DataFrame alignment. If data in both corresponding DataFrame locations is missing the result will be missing. Default is None.

Return Value

Returns the result of the arithmetic operation.

Example: Using floordiv() on whole DataFrame

In the example below, a DataFrame df is created. The floordiv() function is used to divide the whole data frame by a given scalar value.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5, 3, 2, 4],
  "Salary": [60, 62, 65, 59]},
  index= ["John", "Marry", "Sam", "Jo"]
)

print("The DataFrame is:")
print(df)

#dividing all entries of the DataFrame by 2
print("\ndf.floordiv(2) returns:")
print(df.floordiv(2))

The output of the above code will be:

The DataFrame is:
       Bonus  Salary
John       5      60
Marry      3      62
Sam        2      65
Jo         4      59

df.floordiv(2) returns:
       Bonus  Salary
John       2      30
Marry      1      31
Sam        1      32
Jo         2      29

Example: Dividing different column by different value

Different column can be divided by different scalar value by providing other argument as a list. Consider the following example:

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5, 3, 2, 4],
  "Salary": [60, 62, 65, 59]},
  index= ["John", "Marry", "Sam", "Jo"]
)

print("The DataFrame is:")
print(df)

#dividing all entries of Bonus column by 2
#dividing all entries of Salary column by 10
print("\ndf.floordiv([2,10]) returns:")
print(df.floordiv([2,10]))

The output of the above code will be:

The DataFrame is:
       Bonus  Salary
John       5      60
Marry      3      62
Sam        2      65
Jo         4      59

df.floordiv([2,10]) returns:
       Bonus  Salary
John       2       6
Marry      1       6
Sam        1       6
Jo         2       5

Example: Using floordiv() on selected columns

Instead of whole data frame, the floordiv() function can be applied on selected columns. Consider the following example.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5, 3, 2, 4],
  "Last Salary": [58, 60, 63, 57],
  "Salary": [60, 62, 65, 59]},
  index= ["John", "Marry", "Sam", "Jo"]
)

print("The DataFrame is:")
print(df)

#dividing all entries of Salary column by 3
print("\ndf['Salary'].floordiv(3) returns:")
print(df["Salary"].floordiv(3))

#dividing all entries of Salary column by 3
#dividing all entries of Bonus column by 2
print("\ndf[['Salary', 'Bonus']].floordiv([3,2]) returns:")
print(df[["Salary", "Bonus"]].floordiv([3,2]))

The output of the above code will be:

The DataFrame is:
       Bonus  Last Salary  Salary
John       5           58      60
Marry      3           60      62
Sam        2           63      65
Jo         4           57      59

df['Salary'].floordiv(3) returns:
John     20
Marry    20
Sam      21
Jo       19
Name: Salary, dtype: int64

df[['Salary', 'Bonus']].floordiv([3,2]) returns:
       Salary  Bonus
John       20      2
Marry      20      1
Sam        21      1
Jo         19      2

Example: Dividing columns in a DataDrame

The floordiv() function can be applied in a data frame to get the division of two series/column element-wise. Consider the following example.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Dividend": [10, 20, 30, 40, 50],
  "Divisor": [5, 6, 7, 8, 9]
})

print("The DataFrame is:")
print(df)

#dividing 'Dividend' by 'Divisor' column
df['Quotient'] = df['Dividend'].floordiv(df['Divisor'])

print("\nThe DataFrame is:")
print(df)

The output of the above code will be:

The DataFrame is:
   Dividend  Divisor
0        10        5
1        20        6
2        30        7
3        40        8
4        50        9

The DataFrame is:
   Dividend  Divisor  Quotient
0        10        5         2
1        20        6         3
2        30        7         4
3        40        8         5
4        50        9         5

❮ Pandas DataFrame - Functions

5