Home > other >  python - read data from API and save into dataframe
python - read data from API and save into dataframe

Time:12-12

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

CodePudding user response:

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.

  • Related