2013-04-11 34 views
3

我有一個熊貓數據幀,看起來像下面這樣:如何選擇Multiindex數據框中第一級的最後一個鍵的行?

     data 
date  signal      
2012-11-01 a   0.04 
      b   0.03 
2012-12-01 a   -0.01 
      b   0.00 
2013-01-01 a   -0.00 
      b   -0.01 

我想基於多指標的,這是迄今爲止在這種情況下的第一級只得到了最後一排。

2013-01-01 a   -0.00 
      b   -0.01 

第一級索引是datetime。什麼是最優雅的方式來選擇最後一行?

回答

7

一種方法是直接訪問多指標的水平(與使用最後一個):

In [11]: df.index.levels 
Out[11]: [Index([bar, baz, foo, qux], dtype=object), Index([one, two], dtype=object)] 

In [12]: df.index.levels[0][-1] 
Out[12]: 'qux' 

而且隨着ix選擇這些行:(使用@Jeff's example DataFrame

In [13]: df.ix[df.index.levels[0][-1]] 
Out[13]: 
      0   1   2   3 
one 1.225973 -0.703952 0.265889 1.069345 
two -1.521503 0.024696 0.109501 -1.584634 

In [14]: df.ix[df.index.levels[0][-1]:] 
Out[14]: 
       0   1   2   3 
qux one 1.225973 -0.703952 0.265889 1.069345 
    two -1.521503 0.024696 0.109501 -1.584634 

也許更優雅的方式是使用tail(如果你知道那裏會一直存在S爲二):

In [15]: df.tail(2) 
Out[15]: 
       0   1   2   3 
qux one 1.225973 -0.703952 0.265889 1.069345 
    two -1.521503 0.024696 0.109501 -1.584634 
+0

感謝。我其實已經嘗試了你的方法唯一的辦法是返回一個Series,但我希望有一個DataFrame。我提出的醜陋的方法是:df.xs(df.index [-1] [0],level ='date') – ezbentley 2013-04-11 17:05:09

+0

@ezbentley你是什麼意思返回一個系列?我給出的所有三個結果都是DataFrames ...? – 2013-04-11 17:28:39

+0

抱歉抱歉。我很困惑。你的方法返回DataFrames是正確的。 – ezbentley 2013-04-11 18:34:22

3

在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 
0

如果你有一個數據幀df與已經定義,那麼多指標:

df2 = df.ix[df.index[len(df.index)-1][0]] 

也可以工作。

0

你可以得到最後一行iloc

df.iloc[-1] 
相關問題