简体   繁体   中英

How can I turn the following dataframe into a multi-index dataframe?

How can I achieve the following: I have a table like so

|----------------------|
| Date | A | B | C | D |
|------+---+---+---+---|
| 2000 | 1 | 2 | 5 | 4 |
|------+---+---+---+---|
| 2001 | 2 | 2 | 7 | 4 |
|------+---+---+---+---|
| 2002 | 3 | 1 | 7 | 7 |
|------+---+---+---+---|
| 2003 | 4 | 1 | 5 | 7 |
|----------------------|

and turn it into a multi-index type dataframe:

|------------------------------------|
| Column Name | Date | Value | C | D |
|-------------+------+-------+---+---|
|      A      | 2000 |   1   | 5 | 4 |
|             |------+-------+---+---|
|             | 2001 |   2   | 7 | 4 |
|             |------+-------+---+---|
|             | 2002 |   3   | 7 | 7 |
|             |------+-------+---+---|
|             | 2003 |   4   | 5 | 7 |
|-------------+------+-------+---+---|
|      B      | 2000 |   2   | 5 | 4 |
|             |------+-------+---+---|
|             | 2001 |   2   | 7 | 4 |
|             |------+-------+---+---|
|             | 2002 |   1   | 7 | 7 |
|             |------+-------+---+---|
|             | 2003 |   1   | 5 | 7 |
|------------------------------------|

I have tried using the Melt function on a dataframe but could not figure out how to achieve this desired look. I think I would also then have to apply a groupby function to the melted dataframe.

You can use melt with set_index . By adding C and D as id_vars , the columns will keep the same structure, then you can just set the columns of interest as index to get a MultiIndex dataframe:

df.melt(id_vars=['Date', 'C', 'D']).set_index(['variable', 'Date'])

               C  D  value
variable Date             
A        2000  5  4      1
         2001  7  4      2
         2002  7  7      3
         2003  5  7      4
B        2000  5  4      2
         2001  7  4      2
         2002  7  7      1
         2003  5  7      1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM