Pandas层级索引

Pandas层级索引

import pandas as pd
import numpy as np
ser_obj = pd.Series(np.random.randn(12),
                    index=[['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd'],
                           [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]])
print(ser_obj)
a  0    0.078539
   1    0.643005
   2    1.254099
b  0    0.569994
   1   -1.267482
   2   -0.751972
c  0    2.579259
   1    0.566795
   2   -0.796418
d  0    1.444369
   1   -0.013740
   2   -1.541993
dtype: float64

MultiIndex索引对象

print(type(ser_obj.index))
print(ser_obj.index)
<class 'pandas.indexes.multi.MultiIndex'>
MultiIndex(levels=[['a', 'b', 'c', 'd'], [0, 1, 2]],
           labels=[[0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]])

选取子集

外层选取

print(ser_obj['c'])
0    2.579259
1    0.566795
2   -0.796418
dtype: float64

内层选取

print(ser_obj[:, 2])
a    1.254099
b   -0.751972
c   -0.796418
d   -1.541993
dtype: float64

交换分层顺序

print(ser_obj.swaplevel())
0  a    0.078539
1  a    0.643005
2  a    1.254099
0  b    0.569994
1  b   -1.267482
2  b   -0.751972
0  c    2.579259
1  c    0.566795
2  c   -0.796418
0  d    1.444369
1  d   -0.013740
2  d   -1.541993
dtype: float64

交换并排序分层

print(ser_obj.swaplevel().sortlevel())
0  a    0.078539
   b    0.569994
   c    2.579259
   d    1.444369
1  a    0.643005
   b   -1.267482
   c    0.566795
   d   -0.013740
2  a    1.254099
   b   -0.751972
   c   -0.796418
   d   -1.541993
dtype: float64

推荐阅读更多精彩内容