CBSE CS and IP

CBSE Class 11 & 12 Computer Science and Informatics Practices Python Materials, Video Lecture

floordiv() Method of DataFrame

< Back


Use:
Get Integer division of dataframe and other, element-wise.
Prototype:
<DataFrame_Obj>.floordiv (<number> )

import pandas as pd

df = pd.DataFrame({
    'a': [4, 5, 6, 7],
    'b': [10, 20, 30, 40],
    'c': [100, 50, -30, -50]
})

print(df)

   a   b    c
0  4  10  100
1  5  20   50
2  6  30  -30
3  7  40  -50
The floor division functions floordiv() will return the floor division of the DataFrame elements (it is similar to the operator ' // '). Floor division ignores the part after decimal, and give only the integer value. But if the number is negative then the number will be round off to the number bearest to 0.
Ex:- 
 50//3   -> 16
-50//3   -> -17
  
Note:- All the columns should be integer, Otherwise error will occour.

print(df.floordiv(3))

   a   b    c
0  1   3   33
1  1   6   16
2  2  10  -10
3  2  13  -17

No comments:

Post a Comment