1

When I have a pandas.DataFrame df with columns ["A", "B", "C", "D"], I can filter it using constructions like df[df["B"] == 2].

How do I do the equivalent of df[df["B"] == 2], if B is the name of a level in a MultiIndex instead? (For example, obtained by df.groupby(["A", "B"]).mean() or df.setindex(["A", "B"]))

3 Answers 3

1

I would suggest either:

df.xs(2, level='B')

or

df[df.index.get_level_values('B') == val]

I'd like to make the syntax for the latter operation a little nicer.

Sign up to request clarification or add additional context in comments.

Comments

0

I see two ways of getting this, both of which look like a detour – which makes me think there must be a better way which I'm overlooking.

  • Converting the MultiIndex into columns: df[df.reset_index()["B"] == 2]
  • Swapping the name I want to use to the start of the MultiIndex and then use lookup by index: df.swaplevel(0, "B").ix[2]

Comments

0

I think you are looking to grouping by index levels (see GroupBy with MultiIndex).
Here's a short, and not very exciting, example:

In [126]: df = DataFrame([[1,2,3,4],[2,2,np.nan,6]],columns=["A", "B", "C", "D"])

In [127]: df1 = df.set_index(['A','B'])

In [128]: df1
Out[128]: 
      C  D
A B       
1 2   3  4
2 2 NaN  6

In [129]: df1.groupby(level='B', axis=0).mean()
Out[129]: 
   C  D
B      
2  3  5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.