This is my very first post but I'll do my best to make it relevant.
I have a dataframe of stock prices freshly imported with the DataReader, from Morningstar. It looks like this :
print df.head()
Close High Low Open Volume Symbol
Symbol Date
AAPL 2018-03-01 175.00 179.775 172.66 178.54 48801970 AAPL
2018-03-02 176.21 176.300 172.45 172.80 38453950 AAPL
2018-03-05 176.82 177.740 174.52 175.21 28401366 AAPL
2018-03-06 176.67 178.250 176.13 177.91 23788506 AAPL
2018-03-07 175.03 175.850 174.27 174.94 31703462 AAPL
I want to refer to specific cells in the dataframe, especially the values in the last row for a given stock. There are 255 rows.
Please note that the dataframe is a concatenation of multiple DataReader fetches. I made it from code found on StackOverflow with slight updates and changes :
rawdata = [] # initializing empty dataframe
for ticker in tickers:
fetched = web.DataReader(ticker, "morningstar", start='3/1/2018', end='4/15/2018') # bloody month/day/year
fetched['Symbol'] = ticker # add a symbol column
rawdata.append(fetched)
stocks = pd.concat(fetched) # concatenate all the dfs
Now
print df[255:]
returns the last row with column names, and
print df[255:].values
returns the values of the last row. But
print df[-1]
returns an error. I will need to refer to the last row after updating the dataframe, without knowing whether there are now x or y rows. Why can't I do df[-1] ?
I've looked around and found techniques with "iloc" notably, but I'm trying to keep this very simple for the moment.
I've also looked for questions about slicing. But
print df[255:['Close']]
returns the error "unhashable type" - although there already is a column named 'Close'.
Is it because my dataframe is not properly indexed ? Or because it is not a csv yet ? I know how to work on indexes, and also how to write to csv. And I will definitely have to organize the data in a better way at some stage. But I don't understand why I cannot call the last row or slice for a specific cell with the current format of my data.
Thanks for your kind attention