2

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?

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

Oh you even caught NAME 5, which I missed. Thank you
@user2921352 if this answered your question. Don't forget to mark it as the answer, thx.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.