python中熊猫系列的地板或天花板?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27592456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Floor or ceiling of a pandas series in python?
提问by wolfsatthedoor
I have a pandas series series
. If I want to get the element-wise floor or ceiling, is there a built in method or do I have to write the function and use apply? I ask because the data is big so I appreciate efficiency. Also this question has not been asked with respect to the Pandas package.
我有一个熊猫系列series
。如果我想获得按元素划分的地板或天花板,是否有内置方法,还是必须编写函数并使用应用程序?我问是因为数据很大,所以我很欣赏效率。也没有就 Pandas 包提出这个问题。
采纳答案by Alex Riley
You can use NumPy's built in methods to do this: np.ceil(series)
or np.floor(series)
.
您可以使用 NumPy 的内置方法来执行此操作:np.ceil(series)
或np.floor(series)
.
Both return a Series object (not an array) so the index information is preserved.
两者都返回一个 Series 对象(不是数组),因此保留了索引信息。
回答by Jivan
You could do something like this using NumPy's floor, for instance, with a dataframe
:
你可以使用 NumPy 的 floor 来做这样的事情,例如,使用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.
现在无法对其进行测试,但实际可行的解决方案可能离它不远。
回答by wolfsatthedoor
I am the OP, but I tried this and it worked:
我是 OP,但我试过了,它奏效了:
np.floor(series)
回答by jpp
clip_lower
/ clip_upper
clip_lower
/ clip_upper
With Pandas, you can set a floor via clip_lower
or ceiling via clip_upper
:
使用 Pandas,您可以通过以下方式设置地板clip_lower
或天花板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
clip
pd.Series.clip
supports more generalised functionality, e.g. applying and flooring a ceiling simultaneously, e.g. s.clip(-1, 1)
.
pd.Series.clip
支持更通用的功能,例如同时应用和铺设天花板,例如s.clip(-1, 1)
。