我有一个来自backtest的值数据框。样本数据:
market_trading_pair next_future_timestep_return ohlcv_start_date \
0 Poloniex_ETH_BTC 0.003013 1450753200
1 Poloniex_ETH_BTC -0.006521 1450756800
2 Poloniex_ETH_BTC 0.003171 1450760400
3 Poloniex_ETH_BTC -0.003083 1450764000
4 Poloniex_ETH_BTC -0.001382 1450767600
prediction_at_ohlcv_end_date
0 -0.157053
1 -0.920074
2 0.999806
3 0.627140
4 0.999857
我需要写什么来获取2 ohlcv_start_date之间的行,例如
start = 1450756800
结束= 1450767600
会产生第1到第4行
答案 0 :(得分:2)
传递多个布尔条件并使用&
和它们并使用括号作为运算符优先级:
In [189]:
df[(df['ohlcv_start_date'] >=1450756800) & (df['ohlcv_start_date'] <=1450767600)]
Out[189]:
market_trading_pair next_future_timestep_return ohlcv_start_date \
1 Poloniex_ETH_BTC -0.006521 1450756800
2 Poloniex_ETH_BTC 0.003171 1450760400
3 Poloniex_ETH_BTC -0.003083 1450764000
4 Poloniex_ETH_BTC -0.001382 1450767600
prediction_at_ohlcv_end_date
1 -0.920070
2 40.999806
3 0.627140
4 0.999857
答案 1 :(得分:0)
如果为DataFrame提供DatetimeIndex(基于ohlcv_start_date
中的值),那么您可以select rows by date使用df.loc
:
In [61]: df.index = pd.to_datetime(df['ohlcv_start_date'], unit='s')
In [63]: df.loc['2015-12-22 03':'2015-12-22 07']
Out[63]:
market_trading_pair next_future_timestep_return \
ohlcv_start_date
2015-12-22 03:00:00 Poloniex_ETH_BTC 0.003013
2015-12-22 04:00:00 Poloniex_ETH_BTC -0.006521
2015-12-22 05:00:00 Poloniex_ETH_BTC 0.003171
2015-12-22 06:00:00 Poloniex_ETH_BTC -0.003083
2015-12-22 07:00:00 Poloniex_ETH_BTC -0.001382
ohlcv_start_date prediction_at_ohlcv_end_date
ohlcv_start_date
2015-12-22 03:00:00 1450753200 -0.157053
2015-12-22 04:00:00 1450756800 -0.920074
2015-12-22 05:00:00 1450760400 0.999806
2015-12-22 06:00:00 1450764000 0.627140
2015-12-22 07:00:00 1450767600 0.999857