python - 在 Pandas DataFrame 列上应用阈值

标签 python pandas boolean time-series

我有一个看起来像这样的 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
    ...

我想对系列应用一个阈值,以便值低于它,我只需将阈值替换为实际值。

我正在尝试定义一个 boolean 数据框,例如

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

关于python - 在 Pandas DataFrame 列上应用阈值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35745992/

相关文章:

python - 如何将列表应用于 Pandas 组

检查一个数组并返回一个 boolean 值

sql-server - Microsoft SQL Server 中是否有像 MySQL 中那样的 boolean 数据类型?

python - 使用 python 搜索特定重复

python Django : Sending a message from server to client on database save()

python - 将数组和元组元素转换为 Pandas 数据框中的列

Python:计算数据框列中所有行中特定字符的实例

c++ - boolean 函数不能正常工作

Python - 多线程/多处理

python - 在python 3.4中模拟内部对象的实例方法