0

If the dataset contains n=385, now I want to split so each chunk contains the points like 95,95,95,95,5. Now I want to add the last 5 points to previous chunk

I am running on jupyter notebook

def chunks(l, n):
   return [l[i:i+n] for i in range(0, len(l), n)]

slice =chunks(dataframe, int(len(dataframe)/4))

I expected the output into equal sized

Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
Nari Kumar
  • 23
  • 6

3 Answers3

0
def chunks (l, n):
  r = [l]
  while len (r [-1]) >= 2 * n:
    r [-1:] = [r [-1][:n], r [-1][n:]]
  return r
Mikhail Vladimirov
  • 13,572
  • 1
  • 38
  • 40
0

You can add a condition where if the last chunk is not of your desired length, the function adds it to the previous chunk and returns only until that index. For example:

def chunks(l, n):
  foo = [l[i:i+n] for i in range(0, len(l), n)]
  if len(foo[-1]) != n:
    foo[-2]+= foo[-1]
    return foo[:-1]
  else:
    return foo


l = [i for i in range(100)]

chunks = chunks(l, 6)
print(chunks)

Output:

[[0, 1, 2, 3, 4, 5], 
 [6, 7, 8, 9, 10, 11],
...
 [84, 85, 86, 87, 88, 89], 
 [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]]
b-fg
  • 3,959
  • 2
  • 28
  • 44
0

You can update the second last sublist of the list in your existing code by adding the last sublist, and then removing the last sublist, only if the last sublist is not of size n, the chunk size

def chunks(l, n):
   li = [l[i:i+n] for i in range(0, len(l), n)]
   #Add the second last sublist to the last sublist
   #Only if the last sublist is not of size n
   if len(li[-1]) != n:
        li[-2] += li[-1]
        #Remove the last element
        li.pop(-1)
   return li

The output will be

print(chunks([1,2,3,4,5,6,7,8,9],3))
#[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(chunks([1,2,3,4,5,6,7,8,9,10],3))
#[[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40