Python 来自数据帧的多索引数据
我将如何接受这样的df:Python 来自数据帧的多索引数据,python,python-3.x,pandas,Python,Python 3.x,Pandas,我将如何接受这样的df: Dates Type 1 2 3 ... 2018-01-01 Type1 Golf Van Jeep 2018-01-02 T
Dates Type 1 2 3 ...
2018-01-01 Type1 Golf Van Jeep
2018-01-02 Type1 Golf Van Jeep
2018-01-01 Type2 Golf1 Van1 Jeep1
2018-01-02 Type2 Golf2 Van2 Jeep2
Type Type1 Type2
Numbers 1 2 3 1 2 3
Dates ...
2018-01-01 Golf Van Jeep Golf1 Van1 Jeep1
2018-01-02 Golf Van Jeep Golf2 Van2 Jeep2
并将其转化为:
Type1 Type2
Dates 1 2 3 1 2 3 ...
2018-01-01 Golf Van Jeep Golf1 Van1 Jeep1
2018-01-02 Golf Van Jeep Golf2 Van2 Jeep2
编辑:
我想介绍第二个索引如下:
Dates Type 1 2 3 ...
2018-01-01 Type1 Golf Van Jeep
2018-01-02 Type1 Golf Van Jeep
2018-01-01 Type2 Golf1 Van1 Jeep1
2018-01-02 Type2 Golf2 Van2 Jeep2
Type Type1 Type2
Numbers 1 2 3 1 2 3
Dates ...
2018-01-01 Golf Van Jeep Golf1 Van1 Jeep1
2018-01-02 Golf Van Jeep Golf2 Van2 Jeep2
编辑:
现在,如果我想重新标记所有数字索引值,我将如何创建:
Type Type1 Type2
Numbers p1 p2 p3 p1 p2 p3
Dates ...
2018-01-01 Golf Van Jeep Golf1 Van1 Jeep1
2018-01-02 Golf Van Jeep Golf2 Van2 Jeep2
编辑:
您只需使用:
。添加前缀('hh')
与一起使用,然后按更改级别顺序并按以下方式排序多索引
:
编辑:按元组添加:
df = (df.set_index(['Dates','Type'])
.unstack()
.swaplevel(0,1, axis=1)
.sort_index(axis=1)
.rename_axis(('Type','Numbers'), axis=1))
print (df)
Type Type1 Type2
Numbers 1 2 3 1 2 3
Dates
2018-01-01 Golf Van Jeep Golf1 Van1 Jeep1
2018-01-02 Golf Van Jeep Golf2 Van2 Jeep2
IIUC
melt
然后pivot
s=df.melt(['Dates','Type']).pivot_table(index=['Dates'],columns=['Type','variable'],values=['value'],aggfunc='sum')
s.columns=s.columns.droplevel(level=0)
s
Out[189]:
Type Type1 Type2
variable 1 2 3 1 2 3
Dates
2018-01-01 Golf Van Jeep Golf1 Van1 Jeep1
2018-01-02 Golf Van Jeep Golf2 Van2 Jeep2
太好了,谢谢你。我们如何在
类型下添加一个级别来命名numbers@Bob-你能在编辑的问题中显示预期的输出吗?谢谢你,非常感谢你的帮助:)-如果我也要重新标记所有的数字,比如p1,p2,p3。。。。怎么做?啊,我只想。添加前缀('pp')