Search
 
SCRIPT & CODE EXAMPLE
 

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
Comment

PREVIOUS NEXT
Code Example
Python :: scikit learn random forest 
Python :: python range() float 
Python :: how to import functions from another python file 
Python :: merge pdf 
Python :: filter field set in django formds 
Python :: python referenced before assignment in function 
Python :: pandas remove multi header from dataframe 
Python :: replace all occurrences of a value to nan in pandas 
Python :: convert list to dataset python 
Python :: how to print python exception message 
Python :: python add columns to dataframe without changing the original 
Python :: bitwise xor in python 
Python :: index of and last index of in python 
Python :: time zone 
Python :: select statement python 
Python :: remove dict last element 
Python :: python print set 
Python :: parallel iteration python 
Python :: pysimplegui get value from textbox 
Python :: blender change text during animation 
Python :: how to chose right epoch 
Python :: python how to exit function 
Python :: how to find duplicate strings in a list of string python function 
Python :: python match case 
Python :: python strip() 
Python :: split strings around given separator/delimiter 
Python :: python == vs is 
Python :: tree in python 
Python :: python check empty string 
Python :: python count the vowels 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =