Hello Team,
When I download the ROE data for single ticker, I am able download it. code is
import yfinance as yf
UPL= yf.Ticker("UPL.NS")
UPL.get_info()['returnOnEquity']
but when I wants to download it for Multiple ticker simlutaneously I am getting error
tickers = ["ABBOTINDIA.NS", "ACC.NS", "ADANIENT.NS" ,"ADANIPORTS.NS", "ADANITRANS.NS" ,"ALKEM.NS", "AMBUJACEM.NS", "APOLLOHOSP.NS" ,"ASIANPAINT.NS", "AUROPHARMA.NS", "DMART.NS", "AXISBANK.NS"]
ROE = pd.DataFrame()
for t in tickers:
ROE[t] = yf.Ticker[t]
ROE[column = "ROE"] = ROE[t].get_info()['returnOnEquity']
Request you to please get it resolved
Hi Manoj,
You can refer to the code I shared below for your reference.
Also, for your convenience, I've included some comments to clarify the logic.
import yfinance as yf
import pandas as pd
# Variable 'stocks' to store the list of all ticker names
stocks = ["ABBOTINDIA.NS", "ACC.NS", "ADANIENT.NS"] # Feel free to add more
# Create a dataframe called 'data' with two columns
data = pd.DataFrame(columns=['Stock','Return on Equity'])
# A loop that iterates through each ticker in the list passed earlier
for i in stocks:
# Get the data for ticker i and store it in variable ROE
ROE = yf.Ticker(i).get_info()['returnOnEquity']
# Note: In the above, we have first converted i to yf.Ticker object
# Print the ticker name and ROE data at each iteration
print(i, ROE)
# Add the new data to the dataframe 'data'
data = data.append({'Stock': i, 'Return on Equity': ROE}, ignore_index = True)
# Display the dataframe
data
I hope this helps in resolving the problem you were facing.
Thank you so much Sir, It worked.
I have download the ROE data for top 100 stocks, & it took around 5 min.
Is there any faster way to do the same?
Hello Manoj,
Make sure that you separate the code into different cells within your Jupyter Notebook.
For example,
- Cell 1 can include library import statements.
- And, cell 2 can include the actual code to fetch the data.
This would ensure that you are not importing the libraries every time you try to fetch the data.
Also, you can remove the print statement from the above code. Since printing the output is not required unless you really need to visualise what's happening in the background (while the loop runs at each iteration). This will surely save some execution time.
Also, upon researching a bit, I came across a thread that you may find useful. Link to thread.
If you read the answer provided in this thread, it seems as though there is an inherent limitation of using the yfinance library to fetch the data. As pointed out, there could be some bugs or API request restrictions that may possibly be slowing down the download process.
And lastly, if you wish to work with the same dataset, for the time being, I'd recommend you download the data and save it as a CSV file on your local system.
To know more about CSV files, you can check out this interesting practice module on Working with CSV Files on Quantra.
I hope this clarifies your doubt.
All the best and keep learning!