Python Forum
Exporting Stock Fundamental Data to a CSV file with yahoo_fin
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exporting Stock Fundamental Data to a CSV file with yahoo_fin
#3
Hi DustinKlent,

Try the following code.

Best,
Paul

import glob
import numpy as np
import pandas as pd
from yahoo_fin import stock_info as si
#
def getData2(tickers,type=1):
    stats_val = {}
    n=len(tickers)
    for i in range(0,n):
        ticker=tickers.ticker[i]
        if type==1:
            data= si.get_stats_valuation(ticker)
        else:
            data= si.get_stats(ticker)
        data = data.iloc[:,:2]
        data.columns = ["Attribute", "Recent"]
        stats_val[ticker] = data
    combined = pd.concat(stats_val)
    combined = combined.reset_index()
    del combined["level_1"]
    combined.columns = ["Ticker", "Attribute", "Recent"]
    df = pd.DataFrame(combined)
    df2 = df.drop_duplicates(subset=None, keep="first", inplace=False)
    return df2

#
infile='http://datayyy.com/robo/tickers.txt'
tickers=pd.read_table(infile)

df=getData2(tickers)
df2=getData2(tickers,2)

np.shape(df)   #Out[1194]: (36, 3)
np.shape(df2)  # Out[1195]: (204, 3)


"""
input file

ticker
aapl
msft
WMT
IBM

df.head()
Out[1197]: 
  Ticker                  Attribute Recent
0   aapl      Market Cap (intraday)  2.61T
1   aapl           Enterprise Value  2.68T
2   aapl               Trailing P/E  26.86
3   aapl                Forward P/E  25.45
4   aapl  PEG Ratio (5 yr expected)   2.81
""
Reply


Messages In This Thread
RE: Exporting Stock Fundamental Data to a CSV file with yahoo_fin - by paulyan - Aug-01-2022, 06:08 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Create simple live plot of stock data dram 2 2,947 Jan-27-2023, 04:34 AM
Last Post: CucumberNox
  SMA (simple moving avg) Not receiving Data (stock prices). gdbengo 2 1,477 Jul-31-2022, 08:20 PM
Last Post: paulyan
  Yfinance - Intraday stock data with yf.download diogo_80 2 6,047 Apr-29-2022, 05:07 AM
Last Post: processingclouds
  Exporting dataframes to excel without loosing datetime format Rafa 0 1,252 Oct-27-2021, 10:42 AM
Last Post: Rafa
  Exporting a huge dataFrame stylingpat 5 15,720 Mar-23-2021, 12:13 AM
Last Post: stylingpat
  exporting all lines YazeedbnMohmmed 2 2,144 Feb-24-2021, 03:29 AM
Last Post: YazeedbnMohmmed
  Yahoo_fin, Pandas: how to convert data table structure in csv file detlefschmitt 14 7,849 Feb-15-2021, 12:58 PM
Last Post: detlefschmitt
  Yahoo_fin question: how does one export all stock tickers to a csv file detlefschmitt 9 8,825 Feb-12-2021, 07:46 AM
Last Post: detlefschmitt
  xml file creation from an XML file template and data from an excel file naji_python 1 2,133 Dec-21-2020, 03:24 PM
Last Post: Gribouillis
  Python code for exporting table using Selenium gj31980 4 3,041 Aug-04-2020, 01:29 AM
Last Post: gj31980

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020