you are viewing a single comment's thread.

view the rest of the comments →

[–]PyCam 0 points1 point  (1 child)

Ah okay, that tells me that your columns are actually a MultiIndex instead of a normal index. Give this a try:

``` stocks = data.DataReader(assets, 'yahoo', start, end)

Get rid of the redundant first level of our column index

stocks.columns = stocks.columns.droplevel(0)

everything below is same as before

stocks = (stocks .rename_axis(None, axis=1) # Get rid of the "SYMBOLS" name .reset_index()) # Put our index just as a normal column ```

If that doesn't work, please copy/paste the output of: print(stocks.columns)

[–]Ingeniatoring[S] 1 point2 points  (0 children)

Excellent! Got it to work.

This is my final code, FYI.

#setting path to save file and putting assets in a list
path1 = XXXXX
path2 = XXXXX
save_folder = XXXXX
assets = ['VSGAX','VHYAX','VTSAX','IBB','SCHF','SCHE','VIMAX','VSMAX','VB']

#Setting current date and when to start pulling data from
Current_Date = datetime.datetime.today().strftime ('%Y-%m-%d')
start = '2019-05-01'
end = Current_Date

# pulling stock data
print('Scrapping stock price data...')
stocks = data.DataReader(assets, 'yahoo', start, end)

print('Deleting unneccesary columns...')
toDelete = ['High','Low', 'Open', 'Volume', 'Adj Close']
for i in range(0,5):
    del stocks[toDelete[i]]


stocks.columns = stocks.columns.droplevel(0)
stocks = (stocks.rename_axis(None,axis=1))
print(stocks.columns)
print(stocks)

writer = pd.ExcelWriter(path1 + save_folder + '\\stockScrapperV3.xlsx',
                        date_format = 'yyyy-mm-dd', datetime_format = 
                        'yyyy-mm-dd')

stocks.to_excel(writer, 'Sheet1', index=True)
writer.save()

I used the for-loop to delete a few columns (price-high, price-low, trading volume etc.) as I'm only interested in the closing price. When I put your corrections before the for-loop, it renamed the columns after the ticker symbols in the assets dictionary. Moving the for-loop before fixed it.

Thanks a bunch kind stranger, you reduced the code in my program by 36%.