简体   繁体   中英

Access section of pandas data frame that lies between two values

I have a dataframe of values from backtest. Sample data:

 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  

What do i need to write to get the rows between 2 ohlcv_start_date, for example

start = 1450756800

end = 1450767600

would produce rows 1 to 4

pass multiple boolean conditions and use & to and them and use brackets for operator precedence:

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  

If give you DataFrame a DatetimeIndex (based on the value in ohlcv_start_date ) then you can select rows by date using 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  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM