How to split a dataframe into some dataframes according to list of row numbers?

Given a dataframe, I want to obtain a list of distinct dataframes which together concatenate into the original.

The separation is by indices of rows like so

import pandas as pd
import numpy as np

data = {"a": np.arange(10)}
df = pd.DataFrame(data)

print(df)
   a
0  0
1  1
2  2
3  3
4  4
5  5
6  6
7  7
8  8
9  9
separate_by = [1, 5, 6, ]

should give a list of

df1 =

   a
0  0

df2 =

   a
1  1
2  2
3  3
4  4

df3 =

   a
5  5

df4 =

   a
6  6
7  7
8  8
9  9

How can this be done in pandas?



Read more here: https://stackoverflow.com/questions/65076507/how-to-split-a-dataframe-into-some-dataframes-according-to-list-of-row-numbers

Content Attribution

This content was originally published by Gulzar at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: