I have 1000+ text files, each with two columns, one being dates, the other stock prices (at respective date). I have uploaded them and cast them into a dictionary using the following code:
filelist = os.listdir(r'insertfilepath')
filepath = r'insertfilepath'
dic1 = {}
for file in filelist:
df = pd.read_csv(filepath + file,sep='\t')
dic1[file]= df
However, when I try to change the column names for each one using the following code:
for value in dic1:
for df in value:
df.rename(columns={df.columns[0]:'Dates',df.columns[1]:'Prices'},inplace=True)
I get the following error: 'str' object has no attribute 'rename'.
Interestingly this worked when I casted it to a list, but a I can't analyze the data in a list because I have no way of knowing which stock matches what data.
Any help would be greatly appreciated. I'm new to python so please go easy. Thanks
for value in dic1
loops through the keys, which are file name strings in this case. You want for value in dic1.values()
If you want both keys and values, you use dic1.items()
which returns (key, value) tuples