我有以下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(),但我不确定该如何实现。
答案 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
或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