使用负索引时,DataFrame.ix() 似乎没有对我想要的 DataFrame 进行切片。
我有一个 DataFrame 对象,想对最后 2 行进行切片。
In [90]: df = pd.DataFrame(np.random.randn(10, 4))
In [91]: df
Out[91]:
0 1 2 3
0 1.985922 0.664665 -2.800102 1.695480
1 0.580509 0.782473 1.032970 1.559917
2 0.584387 1.798743 0.095950 0.071999
3 1.956221 0.075530 -0.391008 1.692585
4 -0.644979 -1.959265 0.749394 -0.437995
5 -1.204964 0.653912 -1.426602 2.409855
6 1.178886 2.177259 -0.165106 1.145952
7 1.410595 -0.761426 -1.280866 0.609122
8 0.110534 -0.234781 -0.819976 0.252080
9 1.798894 0.553394 -1.358335 1.278704
一种方法:
In [92]: df[-2:]
Out[92]:
0 1 2 3
8 0.110534 -0.234781 -0.819976 0.252080
9 1.798894 0.553394 -1.358335 1.278704
花药的做法:
In [93]: df.ix[len(df)-2:, :]
Out[93]:
0 1 2 3
8 0.110534 -0.234781 -0.819976 0.252080
9 1.798894 0.553394 -1.358335 1.278704
现在我想使用负索引,但有问题:
In [94]: df.ix[-2:, :]
Out[94]:
0 1 2 3
0 1.985922 0.664665 -2.800102 1.695480
1 0.580509 0.782473 1.032970 1.559917
2 0.584387 1.798743 0.095950 0.071999
3 1.956221 0.075530 -0.391008 1.692585
4 -0.644979 -1.959265 0.749394 -0.437995
5 -1.204964 0.653912 -1.426602 2.409855
6 1.178886 2.177259 -0.165106 1.145952
7 1.410595 -0.761426 -1.280866 0.609122
8 0.110534 -0.234781 -0.819976 0.252080
9 1.798894 0.553394 -1.358335 1.278704
如何正确使用带有 DataFrame.ix() 的负索引?谢谢。