内容目录
Pandas层级索引¶
层级索引¶
In [199]:
import numpy as np
import pandas as pd
s1 = 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]])
s1
Out[199]:
a 0 -1.110695 1 -0.432623 2 -1.102602 b 0 -1.163532 1 -0.603245 2 0.643029 c 0 -0.340595 1 -0.881420 2 -1.404732 d 0 -1.687398 1 -0.230343 2 0.069842 dtype: float64
In [203]:
print(s1.index)
MultiIndex([('a', 0), ('a', 1), ('a', 2), ('b', 0), ('b', 1), ('b', 2), ('c', 0), ('c', 1), ('c', 2), ('d', 0), ('d', 1), ('d', 2)], )
选取¶
In [204]:
# 1.外层选取
s1['b']
Out[204]:
0 -1.163532 1 -0.603245 2 0.643029 dtype: float64
In [206]:
# 2.内层选取
s1[:,2]
Out[206]:
a -1.102602 b 0.643029 c -1.404732 d 0.069842 dtype: float64
交换¶
In [208]:
# 将内层索引与外层索引进行交换
# 1.swaplevel()
s2 = s1.swaplevel()
s2
Out[208]:
0 a -1.110695 1 a -0.432623 2 a -1.102602 0 b -1.163532 1 b -0.603245 2 b 0.643029 0 c -0.340595 1 c -0.881420 2 c -1.404732 0 d -1.687398 1 d -0.230343 2 d 0.069842 dtype: float64
In [209]:
s2[:,'a']
Out[209]:
0 -1.110695 1 -0.432623 2 -1.102602 dtype: float64
In [211]:
# 2.sortlevel()先对外层索引进行排序,再对内层索引进行排序