2014-10-22 229 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)