1

I would like to do this with a for loop. My code is:

PC1 = dfA.iloc[0:23]
PC2 = dfA.iloc[23:46]
PC3 = dfA.iloc[46:69]
PC4 = dfA.iloc[69:92]
PC5 = dfA.iloc[92:115]
PC6 = dfA.iloc[115:138]
PC7 = dfA.iloc[138:161]
PC8 = dfA.iloc[161:184]
PC9 = dfA.iloc[184:207]
PC10 = dfA.iloc[207:230]
PC11 = dfA.iloc[230:253]
PC12 = dfA.iloc[253:276]
PC13 = dfA.iloc[276:299]
PC14 = dfA.iloc[299:322]
PC15 = dfA.iloc[322:345]
PC16 = dfA.iloc[345:368]
PC17 = dfA.iloc[368:391]

I tried using a for loop for the numbers in the brackets.

for x in range(0,391,23):
    y = x + 23
    PC[i] = dfA.iloc[x:y]

But I have not found a way that the datasets will be created as in the code above, with i in range(17).

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Alk12
  • 11
  • 2
  • Your starting code uses steps of 23, but your proposed loop uses steps of 28. Is that intentional? Which do you really want? – The Photon Nov 14 '22 at 19:31
  • Thanks for noticing, steps of 23 is what i really want. – Alk12 Nov 14 '22 at 19:40
  • @G.Anderson Thanks for commenting, I do not think that dictionaries will solve my problem as I need dataframes as the final result. – Alk12 Nov 14 '22 at 19:43
  • 1
    Welcome to Stack Overflow. Can you think of a mathematical rule that tells you how to get the value of `i` that you need, given the current value of `x`? What actually is the difficulty? (Hint: list indices start at 0.) – Karl Knechtel Nov 14 '22 at 19:47
  • "I need dataframes as the final result." How does `PC` relate to the intended final result? What will the value of `PC` be before the loop? – Karl Knechtel Nov 14 '22 at 19:48
  • Don't use a dictionary, use a list. Then `PC[16]` (list element) will be the same as `PC17` (variable). – wjandrea Nov 14 '22 at 19:48
  • 2
    @Alk12 "I need dataframes as the final result" -- Yeah, of course. We're saying, put the dataframes in a container (list or dict), instead of separate variables. – wjandrea Nov 14 '22 at 19:51

0 Answers0