frame_1123=pd.DataFrame({'a':range(4),'b':range(4,0,-1),'c':['one','one','two','two'],'d':[0,1,2,3]})
frame_1123
Out[55]:
a b c d
0 0 4 one 0
1 1 3 one 1
2 2 2 two 2
3 3 1 two 3
frame_1224=frame_1123.set_index(['c','d'])
frame_1224
Out[57]:
a b
c d
one 0 0 4
1 1 3
two 2 2 2
3 3 1
frame_1123.set_index(['c','d'],drop=False)
Out[58]:
a b c d
c d
one 0 0 4 one 0
1 1 3 one 1
two 2 2 2 two 2
3 3 1 two 3
frame_1123.reset_index()
Out[59]:
index a b c d
0 0 0 4 one 0
1 1 1 3 one 1
2 2 2 2 two 2
3 3 3 1 two 3
frame_1123.set_index(['c','d'],drop=False,append=True)
Out[60]:
a b c d
c d
0 one 0 0 4 one 0
1 one 1 1 3 one 1
2 two 2 2 2 two 2
3 two 3 3 1 two 3
Python-pandas-DataFrame-多层索引转换
最新推荐文章于 2024-06-13 17:43:07 发布