lundi 12 juillet 2021

Python - Pick 250 random elements from a list of 500 and save it to a new list without making it change each time i run the code

So I have an S&P500 list of tickers that contains 500 companies. I want to generate a new list with random 250 companies chosen from that list and then I want to save it to a new list. What I want to avoid is that each time I run the code, I don't want the program to give me a new set of 250 companies.

import pandas as pd
import yfinance as yf
import random

sp_wiki_url = "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
sp_wiki_df_list = pd.read_html(sp_wiki_url)
sp500_df = sp_wiki_df_list[0]
sp_tickers = list(sp500_df['Symbol'].values)

seq = [i for i in sp_tickers]
subset = random.seed().sample(seq, 250)

This code here will generate a list of 250 companies but it will change each time I run the code, any way to prevent this?

Thanks!




Aucun commentaire:

Enregistrer un commentaire