I have a pandas
DataFrame and I've successfully sampled a row from it, but one of the problems is that it simply returns another DataFrame. I need to read the returned row and get the value of a particular element of that row.
My DataFrame
looks like:
Date Open High Low Close Adj Close Volume
0 1993-01-29 43.968700 43.968700 43.750000 43.937500 26.836645 1003200
1 1993-02-01 43.968700 44.250000 43.968700 44.250000 27.027504 480500
2 1993-02-02 44.218700 44.375000 44.125000 44.343700 27.084740 201300
3 1993-02-03 44.406200 44.843700 44.375000 44.812500 27.371080 529400
4 1993-02-04 44.968700 45.093700 44.468700 45.000000 27.485609 531500
I'm able to sample a row with:
start_state = self.market_data.iloc[:-self._num_trading_days_in_episode].sample(1)
which ends up looking like:
Date Open High Low Close Adj Close Volume
5299 2014-02-13 180.839996 183.199997 180.830002 183.009995 165.017517 100542200
Now I need to figure out how to read the value of Open
from that row.
Use:
start_state = self.market_data.iloc[:-self._num_trading_days_in_episode].sample(1)
First is possible select by labels by DataFrame.loc
with get first value of index:
open = start_state.loc[start_state.index[0], 'Open']
#faster alternative
#open = start_state.at[start_state.index[0], 'Open']
Or by position by DataFrame.iloc
and Index.get_loc
:
pos = market_data.columns.get_loc('Open')
open = start_state.iloc[0, pos]
#faster alternative
#open = start_state.iat[0, pos]
Or select in numpy array first value by indexing:
open = start_state['Open'].values[0]
#pandas 0.24+
#open = start_state['Open'].to_numpy()[0]
Or Series.item
for convert one item Series
to scalar:
open = start_state['Open'].item()
简而言之就是
self.market_data.Open.iloc[:-self._num_trading_days_in_episode].sample(1).iloc[0]
Once you sample you can just call the column you want. Either with loc (label) or with iloc (index)
start_state = self.market_data.iloc[:-self._num_trading_days_in_episode].sample(1)
start_state.loc[:]['Open']
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.