The code below starts by uploading data from yahoo finance in pnls. It does this successufly. Then I have code where I want to add columns to the loaded data in pnls. This part is unsuccessful. Finally I want to save pnls to a csv file which it does successfully if the column changes are not in the code.
The question that I need answered is how do I successfully make multiple column changes before I save pnls to csv??
from pandas_datareader import data as dreader
import pandas as pd
from datetime import datetime
import numpy as np
# Symbols is a list of all the ticker symbols that I am downloading from yahoo finance
symbols = ['tvix','pall','pplt','sivr','jju','nib','jo','jjc','bal','jjg','ld','cdw','gaz','jjn','pgm',
'sgg','jjt','grn','oil','gld','corn','soyb','weat','uhn','uga','bunl','jgbl','bndx','aunz',
'cemb','emhy','vwob','ald','udn','fxa','fxb']
#This gets the data from yahoo finance
pnls = {i:dreader.DataReader(i,'yahoo','1985-01-01',datetime.today()) for i in symbols}
# These are the new columns that I want to add
pnls['PofChg'] = ((pnls.Close - pnls.Open) / (pnls.Open)) * 100
pnls['U_D_F'] = np.where(pnls.PofChg > 0 , 'Up', np.where(pnls.PofChg == 0, 'Flat', 'Down'));pnls
pnls['Up_Down'] = pnls.Close.diff()
pnls['Close%pd'] = pnls.Close.pct_change()*100
# This saves the current ticker to a csv file
for df_name in pnls:
pnls.get(df_name).to_csv("{}_data.csv".format(df_name), index=True, header=True)
This is the error that I get when I run the code
Traceback (most recent call last):
File "YahooFinanceDataGetter.py", line 14, in <module>
pnls['PofChg'] = ((pnls.Close - pnls.Open) / (pnls.Open)) * 100
AttributeError: 'dict' object has no attribute 'Close'
Press any key to continue . . .