层级索引(hierarchical indexing)
下面创建一个Series, 在输入索引Index时,输入了由两个子list组成的list,第一个子list是外层索引,第二个list是内层索引。
让我们认识一下层级索引吧:
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.339618
1 -0.489586
2 -0.849233
b 0 0.638045
1 1.901217
2 -2.009243
c 0 0.469703
1 -0.560258
2 -0.992182
d 0 -0.429164
1 0.596181
2 0.780883
dtype: float64
</