在0.11(本週推出),這是要做到這一點
In [50]: arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux']),
.....: np.array(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'])]
In [51]: df = pd.DataFrame(np.random.randn(8, 4), index=arrays)
In [52]: df
Out[52]:
0 1 2 3
bar one -1.798562 0.852583 -0.148094 -2.107990
two -1.091486 -0.748130 0.519758 2.621751
baz one -1.257548 0.210936 -0.338363 -0.141486
two -0.810674 0.323798 -0.030920 -0.510224
foo one -0.427309 0.933469 -1.259559 -0.771702
two -2.060524 0.795388 -1.458060 -1.762406
qux one -0.574841 0.023691 -1.567137 0.462715
two 0.936323 0.346049 -0.709112 0.045066
In [53]: df.loc['qux'].iloc[[-1]]
Out[53]:
0 1 2 3
two 0.936323 0.346049 -0.709112 0.045066
合理的方式這將在0.10.1
In [63]: df.ix['qux'].ix[-1]
Out[63]:
0 0.936323
1 0.346049
2 -0.709112
3 0.045066
Name: two, dtype: float64
而另一種方式工作(這部作品在0.10。 1),以及
In [59]: df.xs(('qux','two'))
Out[59]:
0 0.936323
1 0.346049
2 -0.709112
3 0.045066
Name: (qux, two), dtype: float64
感謝。我其實已經嘗試了你的方法唯一的辦法是返回一個Series,但我希望有一個DataFrame。我提出的醜陋的方法是:df.xs(df.index [-1] [0],level ='date') – ezbentley 2013-04-11 17:05:09
@ezbentley你是什麼意思返回一個系列?我給出的所有三個結果都是DataFrames ...? – 2013-04-11 17:28:39
抱歉抱歉。我很困惑。你的方法返回DataFrames是正確的。 – ezbentley 2013-04-11 18:34:22