Can't save data from yfinance into a CSV file

那年仲夏 提交于 2020-07-22 14:49:28

问题


I found library that allows me to get data from yahoo finance very efficiently. It's a wonderful library.

The problem is, I can't save the data into a csv file.

I've tried converting the data to a Panda Dataframe but I think I'm doing it incorrectly and I'm getting a bunch of 'NaN's.

I tried using Numpy to save directly into a csv file and that's not working either.

import yfinance as yf
import csv
import numpy as np

urls=[
'voo',
'msft'
    ]

for url in urls:
    tickerTag = yf.Ticker(url)

    print(tickerTag.actions)
    np.savetxt('DivGrabberTest.csv', tickerTag.actions, delimiter = '|')

I can print the data on console and it's fine. Please help me save it into a csv. Thank you!


回答1:


If you want to store the ticker results for each url in different csv files you can do:

for url in urls:
    tickerTag = yf.Ticker(url)
    tickerTag.actions.to_csv("tickertag{}.csv".format(url))

if you want them all to be in the same csv file you can do

import pandas as pd
tickerlist = [yf.Ticker.url for url in urls]
pd.concat(tickerlist).to_csv("tickersconcat.csv")


来源:https://stackoverflow.com/questions/56782173/cant-save-data-from-yfinance-into-a-csv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!