我有一个具有以下结构的DF:
| Level | Rate |
Indicator | AAA | BBB | CCC | XXX | YYY |
location variable |
One 2017 0.69 0.22 0.71 0.02 0.98
2018 0.31 0.15 0.78 0.03 0.96
2019 0.55 0.19 0.82 0.04 0.83
Two 2017 0.31 0.33 0.93 0.11 0.21
2018 0.24 0.35 0.01 0.12 0.14
2019 0.16 0.25 0.12 0.14 0.17
Three 2017 0.58 0.11 0.55 0.21 0.27
2018 0.75 0.10 0.68 0.22 0.25
2019 0.42 0.08 0.71 0.23 0.41
我需要使DF具有以下结构(只有一个级别):
location | variable | Indicator | Level | Rate |
------------------------------------------------
One | 2017 | AAA | 0.69 | NaN |
...
Three | 2019 | YYY | NaN | 0.41 |
我在下面做了几次这样的尝试,但是它们不起作用:
df.melt(col_level=0, id_vars = ['Location','Indicator','variable'] , value_vars = ['Level', 'Rate'])
任何帮助将不胜感激
答案 0 :(得分:1)
将DataFrame.stack
与DataFrame.rename_axis
和DataFrame.reset_index
结合使用:
df = df.stack().rename_axis(('location','variable','indicator')).reset_index()
print (df.head(10))
location variable indicator Level Rate
0 One 2017 AAA 0.69 NaN
1 One 2017 BBB 0.22 NaN
2 One 2017 CCC 0.71 NaN
3 One 2017 XXX NaN 0.02
4 One 2017 YYY NaN 0.98
5 One 2018 AAA 0.31 NaN
6 One 2018 BBB 0.15 NaN
7 One 2018 CCC 0.78 NaN
8 One 2018 XXX NaN 0.03
9 One 2018 YYY NaN 0.96