Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas slicing along multiindex and separate indices

Tags:

python

pandas

I've started using Pandas for some large Datasets and mostly it works really well. There are some questions I have regarding the indices though

  1. I have a MultiIndex with three levels - let's say a, b, c. How do I slice along index a - I just want the values where a = 5, 7, 10, 13. Doing df.ix[[5, 7, 10, 13]] does not work as pointed out in the documentation

  2. I need to have different indices on a DF - can I create these multiple indices and not associate them to a dataframe and use them to give me back the raw ndarray index?

  3. Can I slice a MultiIndex on its own not in a series or Dataframe?

Thanks in advance

like image 651
Wolfgang Kerzendorf Avatar asked Dec 21 '12 16:12

Wolfgang Kerzendorf


1 Answers

For the first part, you can use boolean indexing using get_level_values:

df[df.index.get_level_values('a').isin([5, 7, 10, 13])]

For the second two, you can inspect the MultiIndex object by calling:

df.index

(and this can be inspected/sliced.)

like image 111
Andy Hayden Avatar answered Oct 22 '22 21:10

Andy Hayden