我想基于列price
= P选择列type
中的值,列timestamp
最接近ts
给定的当前时间戳。请做出任何贡献。
输入df trade
amount block_trade_id currency direction index_price instrument_name iv ... price strike tick_direction timestamp trade_id trade_seq type
0 0.2 NaN BTC buy 6107.34 BTC-21MAR20-6125-P 148.99 ... 0.0190 6125 0 1584748972666 42629952 21 P
0 7.1 NaN BTC sell 5428.75 BTC-26JUN20-8000-C 122.21 ... 0.1380 8000 0 1584608399553 42450837 221 C
0 1.0 NaN BTC sell 5743.13 BTC-25SEP20-15000-P 133.16 ... 1.5660 15000 2 1584736336172 42623548 993 P
0 0.6 NaN BTC buy 6185.00 BTC-25SEP20-9000-P 116.23 ... 0.5810 9000 2 1584729697095 42617591 2734 P
0 1.2 NaN BTC sell 6609.72 BTC-3APR20-7750-C 129.47 ... 0.0470 7750 1 1584717196991 42612192 3 C
我的代码:
'''get current timestamp '''
ts = calendar.timegm(time.gmtime())
print(ts)
'''get current Future price'''
idx = trade['timestamp'].sub(ts).abs().idxmin()
fut_price = trade['price'].loc[(trade['type'].loc['P'])&(trade.loc[[idx]])]
答案 0 :(得分:0)
条件选择是通过Boolean
fut_price = trade['price'].loc[(trade['type']=='P')&(trade.index==idx)]