How to slice a python pandas data-frame with 1200 rows into 12 equal parts? either in Python2 or Python 3
Asked
Active
Viewed 2,182 times
0
-
2use `np.array_split(df, 100)` – akash karothiya Jan 13 '17 at 06:16
-
1change 100 to 12, try this `np.array_split(df, 12)` – akash karothiya Jan 13 '17 at 06:28
-
Love this comment thread :) – Paula Livingstone Nov 08 '17 at 23:36
-
Love this comment thread :) too – You Gakukou Nov 24 '17 at 01:38
1 Answers
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