1

I have a dataframe that looks likes this:

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

I want to slice out different ranges of Sweeps.

So for example, I want Sweep0001 : Sweep0003, Sweep0009 : Sweep0015, etc.

I know I can do this in separate lines with ix, i.e.:

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

And then put those back together into one dataframe (I'm doing this so I can average sweeps together, but I need to select some of them and remove others).

Is there a way to do that selection in one line though? I.e. without having to slice each piece separately, followed by recombining all of it into one dataframe.

dan_g
  • 2,712
  • 5
  • 25
  • 44

1 Answers1

0

Use Pandas IndexSlice

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

You can retrieve the labels you want this way:

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 "[]".

In case you want to also slice by columns you can use:

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

More information on slicing is on the Pandas website (http://pandas.pydata.org/pandas-docs/stable/advanced.html#using-slicers) or in this similar Stackoverflow Q/A: Python Pandas slice multiindex by second level index (or any other level)

Community
  • 1
  • 1
raummensch
  • 604
  • 2
  • 8
  • 16