1

May I know if there exist build-in Pandas module to rounds a given number up to the nearest specified multiple.

In excel this can be achieved using Ceiling function.

Say given a df of -0.4, 0.5,1.5, then round this to a multiple of 1 will result in 0,1,and 2.

While I have comb the net about this topic, but I dont see any reference about it. Please let me know if this is a duplicate.

Other might suggest something like here but its not a built in Pandas

mpx
  • 3,081
  • 2
  • 26
  • 56

2 Answers2

1

Maybe this?

> import math as m
> [m.ceil(n) for n in [-0.4, 0.5, 1.5]]
[0, 1, 2]

With dataframe

> import pandas as pd
> df = pd.DataFrame([-0.4, 0.5, 1.5], columns=['numbers'])
> df.apply(m.ceil, axis=1)
0    0
1    1
2    2
dtype: int64

Then without apply

> import numpy as np
> np.ceil(df['numbers'])
0   -0.0
1    1.0
2    2.0
Name: numbers, dtype: float64

Actually, you can also get a similar result only with operations.

> df // 1 + 1
    numbers
0   0.0
1   1.0
2   2.0
nocibambi
  • 2,065
  • 1
  • 16
  • 22
1

I think this was answere here, not specifically with pandas, but with numpy, although I think this is the behavior you are looking for.

sumnuz
  • 45
  • 7