'how to retrieve sector and industry for a list of tickers with python?
I have a list of tickers (below: tick1) that comes from the Earnings Report. I would like to add the "shortname", "sector" and the "industry" next to the ticker while creating a dataframe. Unfortunately, the columns are always shuffeling up a bit and they are not matched properly. for instance: VFC --> sector: technology; industry: Semiconductors, which is wrong. It should be sector: Consumer Cyclical; industry: Apparel Manufacturing
Here is my code below: can you please help to adjust it?
---tickers to be read--- import yfinance as yf
with open("/Users/Doc/AB/Earnings/tickers.txt") as fh:
tick1 = fh.read().split()
tickers in txt file ABOS ACRX ADI ADMP ADOCY AER AGYS AINV ALBO ALLT AMAT AMPS AOZOY ARCO AREC ARZGY ATAI AUTO AVAL AXDX BAH BBAR BBWI BHIL BJ BKYI BLBX BPCGY BPTH BRDS BZFD CAAP CAE CALT CCHWF CCSI CELC CFRHF CGEN CINT CLSN CMRX CRLBF CRXT CSCO CSWI CVSI CWBHF CWBR DAC DADA DE DECK DESP DLO DOYU DTST DUOT EAST EBR EBR.B EDAP ENJY EVTV EXP FATH FL FLO FSI FTK FUV FXLV GAN GBOX GDS GLBE GLOB GNLN GOED GOGL GRAB GRAMF GRCL HD HOOK HPK HUYA HWKN HYRE IBEX IGIC IKT IMPL INLB INLX INVO IONM IONQ IPW IPWR ISUN ITCTY JBI JD JHX JMIA KALA KBNT KEYS KMDA KORE KSLLF KSS KULR LOW LTRY LUNA LVLU MARK MBT MCG MCLD MDWD MDWT MIGI MIRO MNDY MNMD MNRO MSADY MSGM MUFG MVST NEXCF NGS NNOX NOVN NRDY NRGV NU NXGN OBSV OEG OMQS ONON PANW PASG PCYG PEAR PLNHF PLX PTE PTN PXS QIPT QRHC QTEK QUIK RCRT RDY REE REED REKR RKLB RMED RMTI ROST RSKD RYAAY SANW SCVL SDIG SE SHLS SHPW SHWZ SLGG SNPS SPRO SQM SRAD SSYS SUNL SUNW SUPV SYN SYRS TCEHY TCRT TCS TGI TGT THBRF TJX TKOMY TLLTF TME TRMR TSEM TSHA TTWO TXMD USWS VBLT VERB VEV VFC VIPS VJET VOXX VTRU VVOS VWE VYGVF VYNT WEBR WEDXF WEJO WIX WMS WMT WRBY WYY YALA YOU ZIM
---adding the shortname, sector, industry --- from yahooquery import Ticker import pandas as pd
symbols = tick1
tickers = Ticker(symbols, asynchronous=True)
datasi = tickers.get_modules("summaryProfile quoteType")
dfsi = pd.DataFrame.from_dict(datasi).T
dataframes = [pd.json_normalize([x for x in dfsi[module] if isinstance(x, dict)]) for
module in ['summaryProfile', 'quoteType']]
dfsi = pd.concat(dataframes, axis=1)
dfsi
Solution 1:[1]
import pandas as pd
from yahooquery import Ticker
symbols = ['TSHA', 'GRAMF', 'VFC', 'ABOS', 'INLX', 'INVO', 'IONM', 'IONQ']
tickers = Ticker(symbols, asynchronous=True)
datasi = tickers.get_modules("summaryProfile quoteType")
dfsi = pd.DataFrame.from_dict(datasi).T
dataframes = [pd.json_normalize([x for x in dfsi[module] if isinstance(x, dict)]) for
module in ['summaryProfile', 'quoteType']]
dfsi = pd.concat(dataframes, axis=1)
dfsi = dfsi.set_index('symbol')
dfsi = dfsi.loc[symbols]
print(dfsi[['industry', 'sector']])
Output
industry sector
symbol
TSHA Biotechnology Healthcare
GRAMF Drug Manufacturers—Specialty & Generic Healthcare
VFC Apparel Manufacturing Consumer Cyclical
ABOS Biotechnology Healthcare
INLX Software—Application Technology
INVO Medical Devices Healthcare
IONM Medical Care Facilities Healthcare
IONQ Computer Hardware Technology
Try the following. Set the column'symbol' as indexes. And send it to the ticker list. Again, you need to check.
I have run the ticker 'VFC' several times: VFC industry---Apparel Manufacturing, sector---Consumer Cyclical.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | inquirer |