Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

pandas loop over chunk of rows

# credit to Stack Overflow user in the source link
import numpy as np
import pandas as pd

data = pd.DataFrame(np.random.rand(10, 3))
for chunk in np.array_split(data, 5):
  # this assert may not be needed depending on your application
  # left here anyway because it is in the original answer
  assert len(chunk) == len(data) / 5, "This assert may fail for the last chunk if data lenght isn't divisible by 5"
  # do your stuff with each chunk
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #pandas #loop #chunk #rows
ADD COMMENT
Topic
Name
3+8 =