從按級別的 MultiIndex 中選擇
給定以下 DataFrame:
In [11]: df = pd.DataFrame(np.random.randn(6, 3), columns=['A', 'B', 'C'])
In [12]: df.set_index(['A', 'B'], inplace=True)
In [13]: df
Out[13]:
C
A B
0.902764 -0.259656 -1.864541
-0.695893 0.308893 0.125199
1.696989 -1.221131 -2.975839
-1.132069 -1.086189 -1.945467
2.294835 -1.765507 1.567853
-1.788299 2.579029 0.792919
按名稱獲取 A
的值:
In [14]: df.index.get_level_values('A')
Out[14]:
Float64Index([0.902764041011, -0.69589264969, 1.69698924476, -1.13206872067,
2.29483481146, -1.788298829],
dtype='float64', name='A')
或者按級別數量:
In [15]: df.index.get_level_values(level=0)
Out[15]:
Float64Index([0.902764041011, -0.69589264969, 1.69698924476, -1.13206872067,
2.29483481146, -1.788298829],
dtype='float64', name='A')
並針對特定範圍:
In [16]: df.loc[(df.index.get_level_values('A') > 0.5) & (df.index.get_level_values('A') < 2.1)]
Out[16]:
C
A B
0.902764 -0.259656 -1.864541
1.696989 -1.221131 -2.975839
範圍還可以包含多個列:
In [17]: df.loc[(df.index.get_level_values('A') > 0.5) & (df.index.get_level_values('B') < 0)]
Out[17]:
C
A B
0.902764 -0.259656 -1.864541
1.696989 -1.221131 -2.975839
2.294835 -1.765507 1.567853
要提取特定值,你可以使用 xs(橫截面):
In [18]: df.xs(key=0.9027639999999999)
Out[18]:
C
B
-0.259656 -1.864541
In [19]: df.xs(key=0.9027639999999999, drop_level=False)
Out[19]:
C
A B
0.902764 -0.259656 -1.864541