ZacAttack ZacAttack - 1 month ago 11x
Python Question

I can load multiple stock tickers from yahoo finance, but I am having trouble adding new columns before it is saved to a csv

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',

#This gets the data from yahoo finance
pnls = {i:dreader.DataReader(i,'yahoo','1985-01-01', 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 "", 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 . . .


The line

pnls = {i:dreader.DataReader(i,'yahoo','1985-01-01', for i in symbols}

builds a python dict (using dictionary comprehension). To check this, you can run the following:

assert type(pnls) == dict

This is what the error message is telling you: AttributeError: 'dict' object has no attribute 'Close'.

The DataFrames are actually the values of the dictionary. To apply transformations to them, you can iterate over the values() of the dictionary:

for df in pnls.values():
    df = ((df.Close - df.Open) / (df.Open)) * 100