Can't save data from yfinance into a CSV file
Asked Answered
L

1

7

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!

Latour answered 26/6, 2019 at 23:35 Comment(1)
I
5

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")
Interferometer answered 26/6, 2019 at 23:49 Comment(2)
If you found the answer useful, please consider upvoting and accepting the answer (to accept answer, click on check mark symbol).Interferometer
I tried. It says I have less than 15 reputations but won't post publicly until I reached 15 reputations. Thank you though.Latour

© 2022 - 2024 — McMap. All rights reserved.