在 Pandas DataFrame 列上应用阈值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35745992/
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
Apply a threshold on a Pandas DataFrame column
提问by Duccio Piovani
I have a Daframe that looks like this
我有一个看起来像这样的 Daframe
In [52]: f
Out[52]:
Date
2015-02-23 12:00:00 0.172517
2015-02-23 13:00:00 0.172414
2015-02-23 14:00:00 0.172516
2015-02-23 15:00:00 0.173261
2015-02-23 16:00:00 0.172921
2015-02-23 17:00:00 0.172371
2015-02-23 18:00:00 0.176374
2015-02-23 19:00:00 0.177480
...
and I want to apply a threshold to the series so that is the values go below it I would just substitute the threshold's value to the actual one.
我想对系列应用一个阈值,以便值低于它,我只需将阈值替换为实际值。
I am trying to definte a boolean dataframe like
我正在尝试定义一个布尔数据框,例如
Bool = f > Threshold
布尔 = f > 阈值
but I am not sure how to go on. Thanks in Advance.
但我不知道如何继续。提前致谢。
回答by EdChum
IIUC then the following should work:
IIUC 那么以下应该工作:
f[f> Threshold] = some_val
Or you can use clip_upper
:
或者你可以使用clip_upper
:
f = f.clip_upper(Threshold)
This will limit the upper values to your threshold value
这会将上限限制为您的阈值
In [147]:
df[df['val'] > 0.175] = 0.175
df
Out[147]:
val
Date
2015-02-23 12:00:00 0.172517
2015-02-23 13:00:00 0.172414
2015-02-23 14:00:00 0.172516
2015-02-23 15:00:00 0.173261
2015-02-23 16:00:00 0.172921
2015-02-23 17:00:00 0.172371
2015-02-23 18:00:00 0.175000
2015-02-23 19:00:00 0.175000
In [149]:
df['val'].clip_upper(0.175)
Out[149]:
Date
2015-02-23 12:00:00 0.172517
2015-02-23 13:00:00 0.172414
2015-02-23 14:00:00 0.172516
2015-02-23 15:00:00 0.173261
2015-02-23 16:00:00 0.172921
2015-02-23 17:00:00 0.172371
2015-02-23 18:00:00 0.175000
2015-02-23 19:00:00 0.175000
Name: val, dtype: float64