分层索引

分层索引允许在一个轴向上拥有多个索引层级,也就是提供了一种在低纬度处理高纬度数据的方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
data = pd.Series(np.random.randn(9),index=[['a','a','a','b','b','c','c','d','d'],
[1,2,3,1,3,1,2,2,3]])
print(data) ==>
a 1 0.814410
2 0.259285
3 -0.020882
b 1 0.205790
3 0.116076
c 1 2.032459
2 1.170321
d 2 -1.397431
3 1.657013
dtype: float64
print(data.index) ==>
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]])

上面这个例子就是一个以MultiIndex作为索引的Series美化视图

同时通过分层索引对象,允许简介地选出数据的子集:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
data = pd.Series(np.random.randn(9),index=[['a','a','a','b','b','c','c','d','d'],
[1,2,3,1,3,1,2,2,3]])
print(data['b']) ==>
1 -1.630889
3 -0.347437
dtype: float64
print(data['b':'c']) ==>
b 1 -1.630889
3 -0.347437
c 1 -0.542486
2 -1.166458
dtype: float64
print(data.loc[['b','d']]) ==>
b 1 -1.630889
3 -0.347437
d 2 -0.297936
3 0.156680
dtype: float64
#choose in the inner level
print(data.loc[:,2]) ==>
a 0.102767
c -1.166458
d -0.297936
dtype: float64

可以使用unstack方法将数据在DataFrame中重新排列(stack是其反操作)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
data = pd.Series(np.random.randn(9),index=[['a','a','a','b','b','c','c','d','d'],
[1,2,3,1,3,1,2,2,3]])
print(data) ==>
a 1 -1.542656
2 0.228203
3 -1.725217
b 1 0.055736
3 -0.596109
c 1 0.994262
2 -0.538161
d 2 2.523848
3 -0.929541
dtype: float64
print(data.unstack()) ==>
1 2 3
a 1.445621 0.714303 1.091814
b -0.583806 NaN -1.468077
c -0.145544 0.930694 NaN
d NaN 0.302969 -0.427452

DataFrame中每个轴都可以拥有分层索引

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
frame = pd.DataFrame(np.arange(12).reshape((4,3)),
index=[['a','a','b','b'],[1,2,1,2]],
columns=[['Ohio','Ohio','Colorado'],['Green','Red','Green']])
print(frame) ==>
Ohio Colorado
Green Red Green
a 1 0 1 2
2 3 4 5
b 1 6 7 8
2 9 10 11
#接下来的操作是对每个索引进行命名
frame.index.names = ['key1','key2']
frame.columns.names = ['state','color']
print(frame) ==>
state Ohio Colorado
color Green Red Green
key1 key2
a 1 0 1 2
2 3 4 5
b 1 6 7 8
2 9 10 11
Author: YihangBao
Link: https://roarboil.github.io/2019/09/26/classindex/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.