[英]How to apply a threshold to a pandas DataFrame column and output a row outside of the threshold?
[英]Apply a threshold on a Pandas DataFrame column
我有一個看起來像這樣的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
...
並且我想對該系列應用一個閾值,以便值低於它我只是將閾值的值替換為實際值。
我試圖definte一個布爾數據幀,如
Bool = f>閾值
但我不知道該怎么做。 提前致謝。
IIUC然后以下應該工作:
f[f> Threshold] = some_val
或者你可以使用clip_upper
:
f = f.clip_upper(Threshold)
這會將上限值限制為您的閾值
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.