Home > Blockchain >  Create many samples from one dataframe with for loop
Create many samples from one dataframe with for loop

Time:03-24

I have this df create columns l and 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)

Now I would like create 10 samples with 10 rows each:

like sample_1, sample_2 up to sample_10. enter image description here How I make this?

My loop for doesn't work

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

Thanks

CodePudding user response:

It seems your sample_ is not a data container (list or dictionary, for instance). Try the following:

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

Then, access your samples as, e.g.:

>>> 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
  • Related