0

I am trying to loop over a DF to create a smaller DF, then performs other steps to that smaller DF. When completed, I want it to go further down the list to do those same actions until all of the rows in my original DF have been passed through the loop 100 rows at a time ( don't want tuples, or other data formats, i really need each to be in a DF).

i have what I thought was successful code below. start = 0 end = 99 for segment in df: segment = df.loc[start:end] start = start+100 end = end+100

This works for only the first 3 passes and then it stops. i added some code to help diagnose, but I still can't figure out why it's stopping.

print(len(df)
start = 0
end = 99
for segment in df:
    segment = df.loc[start:end]
    print(start,end)
    start = start+100
    end = end+100

results: 2265 0 99 100 199 200 299

Can anyone tell me why it's stopping at row 299? How can I get the loop to keep going through the whole DF (2265 rows)?

Thanks,

Jose Miquel
  • 17
  • 1
  • 6
  • 2
    I assume you have 3 columns in your df and when you do `for segment in df`, `segment` is actually the name of one column (before you reassign its value) anyway, the loop `for` does 3 iterations because 3 columns I think – Ben.T Sep 16 '19 at 18:03

1 Answers1

0

actually that didn't help because it comes up with a list, not a DF (as i stated). however, i was able to find the answer in another thread. Thanks for the help!

Pandas DataFrame slicing with iteration

Jose Miquel
  • 17
  • 1
  • 6