python - read data from API and save into dataframe

117 Views Asked by At

I am trying to read a list of stock ticker and get price from Tiingo, but it reads only the last item "MO" and save into dataframe "data". how can I get the price for a full list? thanks.

lis=[ 
"AAPL",
"MSFT",
"AMZN",
"GOOGL",
"TSLA",
"GOOG",
"NVDA",
"FB",
"JPM",
"UNH",
"HD",
"MO"
]

for i in lis:
    try: 
        data= client.get_dataframe([i],
                                      frequency='daily',
                                      metric_name='close',
                                      startDate='2020-03-01',
                                      endDate='2021-12-10')        
    except:
        pass
1

There are 1 best solutions below

0
On BEST ANSWER

You're overwriting data on every iteration.

Try having data as a list:

data = []
    
for i in lis:
    try: 
        data.append(client.get_dataframe([i],
                                      frequency='daily',
                                      metric_name='close',
                                      startDate='2020-03-01',
                                      endDate='2021-12-10'))      
    except:
        pass

And I highly discourage using try... except: pass. Can lead to lots of different issues.