0

How to split a dataframe into exactly n parts, irrespective of size of the frame?

if I try:

n = int(len(df.index)/662) 
chunk_list = [df [ i : i+n ] for i in range(0, df.shape[0], n)] 

I get some chunk_lists of size 663 and some of 664. How do I get all dataframes to split into exactly 663 parts?

Mary
  • 14,926
  • 3
  • 18
  • 27

1 Answers1

0

Ok I was able to resolve this. I referred Splitting a list into N parts of approximately equal length

So I did

chunk_list = [df[(i*len(df.index))//663:((i+1)*len(df.index))//663] for i in range(663)] 

and was able to resolve the issue

sophocles
  • 13,593
  • 3
  • 14
  • 33