使用 for 循环从一个数据帧创建多个样本

Create many samples from one dataframe with for loop

我有这个df 创建列 l 和 l1:

l1=[]
for i in range(1005,1105):
    l1.append(i)

l=[]
for i in range(1005,1105):
    l.append(i)

d = {'col1': l1, 'col2': l}
df = pd.DataFrame(data=d)

现在我想创建 10 个样本,每个样本 10 行:

喜欢 sample_1,sample_2 最多 sample_10。 enter image description here 我是怎么做到的?

我的循环不工作

for j in [1,2,3,4,5,6,7,8,9,10]: 
    sample_[j] = df.sample(n=10, random_state=j)

谢谢

您的 sample_ 似乎不是数据容器(例如列表或字典)。请尝试以下操作:

sample_ = {}
for j in [1,2,3,4,5,6,7,8,9,10]: 
    sample_[j] = df.sample(n=10, random_state=j)

然后,访问您的样本,例如:

>>> sample_[1]

    col1  col2
80  1085  1085
84  1089  1089
33  1038  1038
81  1086  1086
93  1098  1098
17  1022  1022
36  1041  1041
82  1087  1087
69  1074  1074
65  1070  1070