![](https://csdnimg.cn/release/download_crawler_static/86904365/bg1.jpg)
数据规整:连接、联合与重塑
In[]:
8.1 分层索引
分层索引是在一个轴有两层或以上的索引。分层索引提供了低纬度处理更高维度数据的方式
In[]:
In[]:
In[]:
In[]:
In[]:
In[]:
import numpy as np
import pandas as pd
pd.options.display.max_rows = 20
np.random.seed(12345)
import matplotlib.pyplot as plt
plt.rc('figure', figsize=(10, 6))
np.set_printoptions(precision=4, suppress=True)
data = pd.Series(np.random.randn(9).round(2),
index=[['a', 'a', 'a', 'b', 'b', 'c', 'c', 'd', 'd'],
[1, 2, 3, 1, 3, 1, 2, 2, 3]])
data
data.index#分层索引是元组的有序结合。
mindex=pd.MultiIndex(levels=[['a', 'b', 'c', 'd'],[1,2,3]],
codes=[[0,0,0,1,1,2,2,3,3],[0,1,2,0,2,0,1,1,2]],
names=['lev1','lev2'])
data = pd.Series(np.random.randn(9).round(2),
index=mindex)
data
data['b']
data['b':'c']