简体   繁体   中英

How to save yfinance data to csv?

Okay so I'm trying to get all the information using yfinance, but the problem is that I'm not able to store all that data in a single file. My client wants me to get all the data for every stock, so I tried creating a program that allows you to enter a certain stock and get data.

def get_info_on_stock( ticker):
    stock = yf.Ticker(ticker)
    hist = stock.history(period = 'max')
    action = stock.actions
    dividend = stock.dividends
    financial = stock.financials
    qtr_financials = stock.quarterly_financials
    major_stake_holders = stock.major_holders
    institutional_holders = stock.institutional_holders
    df = print(stock, hist, action, dividend, financial, qtr_financials, major_stake_holders, 
    institutional_holders)
    df.actions.to_csv("data{}.csv")
      

can someone please help and guide me? I'm a beginner at this I could really use some pointers.

It looks like you are trying to save multiple dataframes in one single csv file. I strongly suggest to save multiple files instead.

First of all, your variables action and dividend are included in hist . So two files less to think about. In total you'll have:

# replace your last two lines with:
hist.to_csv(f"{ticker}_hist.csv")
financial.to_csv(f"{ticker}_financial.csv")
qtr_financials.to_csv(f"{ticker}_qtr_financials.csv")
major_stake_holders.to_csv(f"{ticker}_major_stake_holders.csv")
institutional_holders.to_csv(f"{ticker}_institutional_holders.csv")

But this is very unpractical. What you probably want to do is to easily manage multi-dimensions data. To do this I encourage you to look something like python xArray

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