Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't save data from yfinance into a CSV file

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!

like image 724
Peter Avatar asked Jun 26 '19 23:06

Peter


People also ask

Why is my CSV not importing correctly?

The most common CSV import errors include: The file size is too large - The CSV import tool of the program you're using might have a file size requirement. To reduce the file size, you can delete unnecessary data values, columns, and rows.

Why do CSV files not save formatting?

CSV files contain only data, as comma-separated values. If you want to keep your formatting changes, save the file as an Excel file (i.e. myfile. xls), using the 'save as' file menu option.

How do I save a Jupyter notebook output as a CSV?

If you have data in pandas DataFrame then you can use . to_csv() function from pandas to export your data in CSV .


1 Answers

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")
like image 68
Juan Carlos Ramirez Avatar answered Sep 20 '22 17:09

Juan Carlos Ramirez