Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply a threshold on a Pandas DataFrame column

I have a Daframe that looks like this

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

but I am not sure how to go on. Thanks in Advance.

like image 353
Duccio Piovani Avatar asked Dec 19 '22 19:12

Duccio Piovani


1 Answers

IIUC then the following should work:

f[f> Threshold] = some_val

Or you can use 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
like image 192
EdChum Avatar answered Dec 21 '22 10:12

EdChum