Suppose I have the following dataframe :
X Y
---+---+---+---
A | B | A | B
--+---+---+---+---
0 | 1 | 2 | 3 | 4
1 | 5 | 6 | 7 | 8
2 | 9 | 10| 11| 12
I want to split it based on the multilevel index recursively and save them in a csv file.
For example a file name X_A.csv should contain the following dataframe:
X
---
A
--+---
0 | 1
1 | 5
2 | 9
Similarly the file X_B.csv should store a dataframe as :
X
---
B
--+---
0 | 2
1 | 6
2 | 10
and so on for Y_A and Y_B.
I am looking for a Pythonic ( or efficient) way to do this rather than iterating over the column values separately as the code is quite large. I tried using the techniques mentioned here by dropping the column levels and storing the individual columns but I want to it in such a way that I don't have to explicitly mention the column names since the dataframe may expand ( i.e. at the top most level there might be 4 columns say W, X, Y and Z).