【问题标题】:Change row values based on condition根据条件更改行值
【发布时间】:2019-02-26 00:56:30
【问题描述】:

我有以下数据框:

>>> BA_df
                   'BID_price' 'ASK_price'
2017-06-01-09:30   100.00      101.50
2017-06-01-09:35   102.00      101.80
2017-06-01-09:40   101.65      101.82

如果BID_price 大于或等于ASK_price,我想做的是使两行都为零。否则,请保持原样。 I checked out this question, which uses numpy.select(),但我不确定如何在我的情况下实现它。

【问题讨论】:

    标签: python-3.x pandas


    【解决方案1】:

    过滤和reindex

    df=df.query('BID_price<=ASK_price').reindex(df.index,fill_value=0)
    df
    Out[724]: 
                      BID_price  ASK_price
    2017-06-01-09:30     100.00     101.50
    2017-06-01-09:35       0.00       0.00
    2017-06-01-09:40     101.65     101.82
    

    where (mask)

    df=df.where(df.BID_price<=df.ASK_price,0)
    df
    Out[726]: 
                      BID_price  ASK_price
    2017-06-01-09:30     100.00     101.50
    2017-06-01-09:35       0.00       0.00
    2017-06-01-09:40     101.65     101.82
    

    【讨论】:

    • 谢谢!当您知道如何操作时看起来很简单:p
    • @scrps93 是的。它是:-) 快乐编码
    猜你喜欢
    • 2021-11-28
    • 1970-01-01
    • 2014-10-09
    • 2020-01-02
    • 1970-01-01
    • 2022-11-30
    • 2013-08-28
    • 2013-07-22
    • 2013-12-27
    相关资源
    最近更新 更多