我有一个如下所示的数据框:
import pandas as pd
d = {'decil': ['1. decil','1. decil','2. decil','2. decil','3. decil','3. decil'],
'kommune': ['AA','BB','AA','BB','AA','BB'],'2010':[44,25,242,423,845,962],
'2011':[64,26,239,620,862,862]}
df = pd.DataFrame(data=d)
印刷
decil kommune 2010 2011
1. decil AA 44 64
1. decil BB 25 26
2. decil AA 242 239
2. decil BB 423 620
3. decil AA 845 862
3. decil BB 962 862
我想要的输出是这样的
kommune year 1. decil 2. decil 3. decil
AA 2010 44 242 845
AA 2011 64 239 862
BB 2010 25 423 962
BB 2011 25 620 862
也就是说,我正在寻找一种方法,将'decil'列从长格式改为宽格式,同时将年份列从宽格式改为长格式。我试过了pd.pivot表,没有任何运气的循环和展开。有什么好办法吗?提前,谢谢你的帮助。你知道吗
将^{} 与^{} 和^{} 一起使用:
相关问题 更多 >
编程相关推荐