1

Take the following code:

df = pd.DataFrame([range(5),range(5),range(5),range(5)], columns=range('a','e'))
df.set_index(['a','b','c','d'])
subselection = df[0, 2]

The second line of code is supposed to fetch the sub-dataframe according to the first level index and second level index.

This doesn't work for me. How do I do it?

Brian
  • 2,163
  • 1
  • 14
  • 26
wlad
  • 2,073
  • 2
  • 18
  • 29

1 Answers1

1

Use DataFrame.xs with tuples:

subselection = df.xs((0, 1), level=[0, 1])
print (subselection)
     e
c d   
2 3  4
  3  4
  3  4
  3  4
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252