2014-10-22 64 views
1

的多個子範圍我有一個數據幀,看起來喜歡這樣的:大熊貓多指標DF - 切片索引

Sweep  Index 
Sweep0001 0  -70.434570 
      1  -67.626953 
      2  -68.725586 
      3  -70.556641 
      4  -71.899414 
      5  -69.946289 
      6  -63.964844 
      7  -73.974609 
... 
Sweep0039 79985 -63.964844 
      79986 -66.406250 
      79987 -67.993164 
      79988 -68.237305 
      79989 -66.894531 
      79990 -71.411133 

我想切出掃描的不同範圍。

因此,舉例來說,我想Sweep0001:Sweep0003,Sweep0009:Sweep0015等

我知道我可以在單獨的行與九做到這一點,即:

df.ix['Sweep0001':'Sweep0003'] 
df.ix['Sweep0009':'Sweep0015'] 

然後把那些回合併爲一個數據框(我正在這樣做,這樣我可以平均掃描一起,但我需要選擇其中的一部分並刪除其他部分)。

雖然有一種方法可以在一行中進行選擇嗎?即而不必分別分片,然後將其全部重新組合到一個數據幀中。

回答

0

使用熊貓IndexSlice

import pandas as pd 
idx = pd.IndexSlice 
df.loc[idx[["Sweep0001", "Sweep0002", ..., "Sweep0003", "Sweep0009", ..., "Sweep0015"]] 

您可以檢索你想要的標籤是這樣的:

list1 = df.index.get_level_values(0).unique() 
list2 = [x for x in list1] 
list3 = list2[1:4] #For your Sweep0001:Sweep0003 
list3.extend(list2[9:16]) #For you Sweep0009:Sweep0015 
df.loc[idx[list3]] #Note that you need one set of "[]" 
        #less around "list3" as this list comes 
        #by default with its own set of "[]". 

如果你也想被列切,你可以使用:

df.loc[idx[list3],:] #Same as above to include all columns. 
df.loc[idx[list3],:"column label"] #Returns data up to that "column label". 

有關切片的更多信息,請登錄Pandas網站(http://pandas.pydata.org/pandas-docs/stable/advanced.html#using-slicers)或在此類似Stackoverflow Q/A:Python Pandas slice multiindex by second level index (or any other level)