Pandas_分层索引

DataFrame分层索引

frame=pd.DataFrame(np.arange(12).reshape(4,3),
                   index=[['a','b','a','b'],[1,2,1,2]],
                   columns=[['ohio','ohio','colo'],['red','red','yellow']])
#分层索引取名
frame.index.names=['key1','key2']
frame.columns.names=['state','color']
print(frame)
'''
state     ohio       colo
color      red red yellow
key1 key2                
a    1       0   1      2
b    2       3   4      5
a    1       6   7      8
b    2       9  10     11
'''

层级排序(重新赋值)

'''
层级排序,不会改变值,只会改变层级位置
swaplevel参数:
i,j:层级序号或名称
axis=0
'''
frame=pd.DataFrame(np.arange(12).reshape(4,3),
                   index=[['a','b','a','b'],[1,2,1,2]],
                   columns=[['ohio','ohio','colo'],['red','red','yellow']])
frame.index.names=['key1','key2']
frame.columns.names=['state','color']
frame1=frame.swaplevel('key1','key2')
print(frame1)
'''
state     ohio       colo
color      red red yellow
key2 key1                
1    a       0   1      2
2    b       3   4      5
1    a       6   7      8
2    b       9  10     11
'''
frame=pd.DataFrame(np.arange(12).reshape(4,3),
                   index=[['a','b','a','b'],[1,2,1,2]],
                   columns=[['ohio','ohio','colo'],['red','red','yellow']])
frame.index.names=['key1','key2']
frame.columns.names=['state','color']
frame1=frame.sort_index(level=-1)
print(frame1)
'''
state     ohio       colo
color      red red yellow
key1 key2                
a    1       0   1      2
     1       6   7      8
b    2       3   4      5
     2       9  10     11
'''

按层级统计

frame=pd.DataFrame(np.arange(12).reshape(4,3),
                   index=[['a','b','a','b'],[1,2,1,2]],
                   columns=[['ohio','ohio','colo'],['red','red','yellow']])
frame.index.names=['key1','key2']
frame.columns.names=['state','color']
frame1=frame.sum(level='color',axis=1)
print(frame1)
'''
color      red  yellow
key1 key2             
a    1       1       2
b    2       7       5
a    1      13       8
b    2      19      11
'''

列索引(重新赋值)

'''
将某一列或几列作为行索引
set_index(self, keys, drop=True, append=False, inplace=False,
                  verify_integrity=False)
drop:列从DataFrame中移除
'''
frame=pd.DataFrame({
    'a':range(4),
    'b':range(3,-1,-1),
    'c':['a','b','c','d'],
    'd':['one','two','three','four']
})
frame2=frame.set_index(['c','d'])
print(frame2)
'''
         a  b
c d          
a one    0  3
b two    1  2
c three  2  1
d four   3  0
'''


'''
reset_index():set_index()的反操作
'''

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值