birdmw
birdmw

Reputation: 875

Split Python Dataframe into multiple Dataframes (where chosen rows are the same)

I would like to split one DataFrame into N Dataframes based on columns X and Z where they are the same (as eachother by column value).

For example, this input:

df = 
   NAME X Y Z Other
0  a    1 1 1 1
1  b    1 1 2 2
2  c    1 2 1 3
3  d    1 2 2 4
4  e    1 1 1 5
5  f    2 1 2 6
6  g    2 2 1 7
7  h    2 2 2 8
8  i    2 1 1 9
9  j    2 1 2 0

Would have this output:

df_group_0 = 
   NAME X Y Z Other
0  a    1 1 1 1
2  c    1 2 1 3
4  e    1 1 1 5

df_group_1 = 
   NAME X Y Z Other
1  b    1 1 2 2
3  d    1 2 2 4

df_group_2 = 
   NAME X Y Z Other
6  g    2 2 1 7
8  i    2 1 1 9

df_group_3 = 
   NAME X Y Z Other
7  h    2 2 2 8
9  j    2 1 2 0

Is this possible?

Upvotes: 2

Views: 1872

Answers (1)

akuiper
akuiper

Reputation: 214957

groupby generates an iterator of tuples with the first element be the group id, so if you iterate through the groupers and extract the second element from each tuple, you can get a list of data frames each having a unique group:

grouper = [g[1] for g in df.groupby(['X', 'Z'])]

grouper[0]
NAME    X   Y   Z   Other
0   a   1   1   1   1
2   c   1   2   1   3
4   e   1   1   1   5

grouper[1]
NAME    X   Y   Z   Other
1   b   1   1   2   2
3   d   1   2   2   4

grouper[2]
NAME    X   Y   Z   Other
6   g   2   2   1   7
8   i   2   1   1   9

grouper[3]
NAME    X   Y   Z   Other
5   f   2   1   2   6
7   h   2   2   2   8
9   j   2   1   2   0

Upvotes: 3

Related Questions