根据条件更改行值

时间:2019-02-25 16:54:06

标签: python-3.x pandas

我有以下DataFrame:

>>> 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(),但我不确定该如何实现。

1 个答案:

答案 0 :(得分: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

wheremask

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