0

How to slice a python pandas data-frame with 1200 rows into 12 equal parts? either in Python2 or Python 3

khangaroth
  • 75
  • 3
  • 13

1 Answers1

2

You can do it several ways. I'd use groupby and a dictionary comprehension. Even then, there are two obvious and distinct ways to split it.

Consider the dataframe df

df = pd.DataFrame(dict(A=np.arange(1200)))

contiguous
Meaning, grab the first 100, then the next, so on and so forth

twelve_equal_dfs_contiguous = \
    {name: group for name, group in df.groupby(np.arange(1200) // 100)}

stratified
Meaning, grab every other 100 starting with the first. Then repeat starting with the second, so on and so forth

twelve_equal_dfs_stratified = \
    {name: group for name, group in df.groupby(np.arange(1200) % 100)}

There are too many ways to do this. Hopefully this is some guidance on where to start.

piRSquared
  • 285,575
  • 57
  • 475
  • 624