1

I'm really struggling to organise my data into 'bins' in Jupyter Notebook. I need the Length column to be rounded UP to the next 10 but I can only seem to round it up to the nearest whole number. I would really appreciate some guidance. Thanks in advance. :)

IN[58]  df2['Length']

OUT[58] 0    541.56
        1    541.73
        2    482.22
        3    345.45
...

Needs to look something like this:

IN[58]  df2['Length']

OUT[58] 0    550
        1    550
        2    490
        3    350
...
mikem11
  • 23
  • 3

1 Answers1

1

Sample

print (df2)
   Length
0  541.56
1  541.73
2  482.22
3  500.00 <- whole number for better sample 

You can use integer division, mutiple by 10 and convert to integers and add 10 if modulo is not 0:

s = (df2['Length'] // 10 * 10).astype(int) + (df2['Length'] % 10 > 0).astype(int) * 10
print (s)
0    550
1    550
2    490
3    500
Name: Length, dtype: int32

Another one use fact // round down:

s = -(-df2['Length'] // 10 * 10).astype(int)
print (s)
0    550
1    550
2    490
3    500
Name: Length, dtype: int32

Or is possible use division with np.ceil:

s = (np.ceil(df2['Length'] / 10) * 10).astype(int)
print (s)
0    550
1    550
2    490
3    500
Name: Length, dtype: int32
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252