Here is my code.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import quandl
start = pd.to_datetime('2012-01-01')
end = pd.to_datetime('2017-01-01')
aapl = quandl.get('WIKI/aapl.11', start_date = start, end_date= end )
csco = quandl.get('WIKI/csco.11', start_date = start, end_date= end )
ibm = quandl.get('WIKI/ibm.11', start_date = start, end_date= end )
amzn = quandl.get('WIKI/amzn.11', start_date = start, end_date= end )
This creates 4 data frames. I want to be able to achieve this by using a for loop.
Here is what I imagine the for loop would look like.
for i in [aapl,csco,ibm,amzn]:
a = 'WIKI/'+ i + '.11'
i = quandl.get(a, start_date=start, end_date=end)
I would like to be able to reference the name of the data frame as a string in the loop to perform other functions that require the name of the dataframe as a string.
Anyone help me with this or suggest an alternative approach which achieve the same result. I am hoping to be able to do this in a way that would scale to 100s of loop iterations.
Thank you for your help.
Equivalent to G. B.'s answer, but without dictionary comprehension in case you are not familiar with it yet.
import pandas as pd
import quandl
start = pd.to_datetime('2012-01-01')
end = pd.to_datetime('2017-01-01')
data = {}
for key in ['aapl', 'csco', 'ibm', 'amzn']:
name = 'WIKI/'+ key + '.11'
data[key] = quandl.get(name, start_date=start, end_date=end)
# Then you can use it like
data['aapl'].DoSomething()