How to download latest NIFTY 50 Stocks List 2022 using just two lines of Python Code?
In this tutorial, we will use python to download the latest NIFTY 50 stocks. We will be using the official NSE website to source the link to the CSV File. Hence, this method will be useful to download the latest Nifty 50 stocks no matter when are you using this tutorial.
Python code to download NIFTY 50 Stocks List 2022
We will be using Python and Jupyter Notebook for downloading the NIFTY 50 stocks list. We will be using following modules to download and saving the list of Nifty 50 stocks:
Further, we will be using the download csv link from NSE’s official website to download the CSV file and read it as a pandas dataframe.
So open a terminal and run the following command.
This will open the jupyter notebook in your default browser. In the Jupyter notebook create a new python file. and add the following code:-
import pandas as pd import pickle URL = 'https://www1.nseindia.com/content/indices/ind_nifty50list.csv' df = pd.read_csv(URL, index_col = 'Company Name') df
Here, we have read the csv file directly into Pandas and created a Pandas Dataframe, further, we have set ‘Company Name’ as Index. To learn more about setting an index of the Pandas dataframe, you can read this post. Running the code will give you following result:-
Here, you can see that, we have got the list of all the NIFTY 50 stocks from csv and it has Company Name, Industry, Symbol, Series and ISIN code of NIFTY 50 stocks.
How to extract a list of NIFTY 50 stock symbols from the CSV.
Further, in most of the cases, you will be needing just the symbol of the stocks, so we will be extracting the symbols and creating a Python List.
nifty_50_list = df['Symbol'].tolist() nifty_50_list
This, will give us a list of NIFTY 50 stocks symbols which can be used anytime.
Creating a pickle file of NIFTY 50 stocks for future use
Thereafter, we will creating a pickle of the list, so that it can be used in any of the projects in future. To do that, we have to use the following code:-
with open('NIFTY50.pickle', 'wb') as f: pickle.dump(nifty_50_list, f)
This will create a pickle file which can be use in any of your project and to extract the list, you will simply have to use the following code:-
with open('NIFTY50.pickle', 'rb') as f: nifty_50 = pickle.load(f)