Floor or ceiling of a pandas series in python?

我与影子孤独终老i 提交于 2019-11-30 04:18:38

You can use NumPy's built in methods to do this: np.ceil(series) or np.floor(series).

Both return a Series object (not an array) so the index information is preserved.

You could do something like this using NumPy's floor, for instance, with a dataframe:

floored_data = data.apply(np.floor)

Can't test it right now but an actual and working solution might not be far from it.

I am the OP, but I tried this and it worked:

np.floor(series)

clip_lower / clip_upper

With Pandas, you can set a floor via clip_lower or ceiling via clip_upper:

s = pd.Series([-1, 0, -5, 3])

print(s.clip_lower(0))
# 0    0
# 1    0
# 2    0
# 3    3
# dtype: int64

print(s.clip_upper(0))
# 0   -1
# 1    0
# 2   -5
# 3    0
# dtype: int64

clip

pd.Series.clip supports more generalised functionality, e.g. applying and flooring a ceiling simultaneously, e.g. s.clip(-1, 1).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!