简体   繁体   中英

python - read data from API and save into dataframe

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM