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

1
  • 2
    np.ceil(df) or np.ceil(serie) Commented Oct 14, 2020 at 8:02

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the suggestion, but the OP present the data in term of dataframe.
It seems external module is still require then.
@balandongiv, maybe not, I added one without external modules.
plus one for that, maybe that answered why there is no built in function for this.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.