随机选择标准普尔500指数公司的100家公司数据



我得到了三个csv文件,其中一个包含S&P 500指数成分股公司,另外两家公司包含其交易量和回报数据。我如何在Python 中从这500家公司中随机选择100家

from random import seed
from random import choice
seed(2)
numbers = [i for i in range(100)]
print(data)
for _ in range(50):
selection = choice(numbers)
print(selection)

使用random.cochoice((可能会导致重复样本,因为它是带替换的随机采样。

试试这个:

使用另一个列表中指定数量的随机元素创建新列表。

from random import sample
numbers = [i for i in range(500)]
hundred_selected_numbers = sample(numbers, 100)

最新更新