Create a MultiIndex dataframe from dataframe

41 Views Asked by At

I have a dataframe which is formatted like in the picture below, the numbers ranging from 0 to 874. Extract of the dataframe I am trying to get a multiindex dataframe from it with the first element of the tuple as the "super" index and the second one as the "lower" index. Could anyone help me?

1

There are 1 best solutions below

0
user19077881 On

To use a simple example: if you have the DataFrame:

   (x, 1)  (y, 2)
0       1       4
1       2       5
2       3       6

the use

df.columns = pd.MultiIndex.from_tuples(df.columns)

to produce the DataFrame

   x  y
   1  2
0  1  4
1  2  5
2  3  6