Python 位于两个值之间的数据帧的访问部分
我有一个来自backtest的值的数据帧。样本数据:Python 位于两个值之间的数据帧的访问部分,python,pandas,Python,Pandas,我有一个来自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 Pol
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\u开始日期之间的行
开始=1450756800
结束=1450767600
将生成第1行到第4行传递多个布尔条件,并使用
&
和它们,并使用括号作为运算符优先级:
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
[189]中的
df[(df['ohlcv_start_date']>=1450756800)和(df['ohlcv_start_date']如果给数据帧一个DatetimeIndex(基于ohlcv_start_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
df[(df['ohlcv_start_date']>=1450756800)和(df['ohlcv_start_date']工作得很好,谢谢!我真的需要学习熊猫而不是在这里问问题,我不是真的在学习。