Pandas Multiindex df - 切割索引的多个子范围

时间:2014-10-22 03:48:23

标签: python pandas slice multi-index

我有一个看起来像这样的数据框:

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等。

我知道我可以用ix分开,即:

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

然后将它们放回到一个数据帧中(我这样做,所以我可以将扫描平均,但我需要选择其中一些并删除其他数据帧)。

有没有办法在一行中进行选择?即无需分别切片,然后将所有部分重新组合成一个数据帧。

1 个答案:

答案 0 :(得分:0)

使用Pandas 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)