Eduard Geist
Eduard Geist

Reputation: 43

Select multi-index columns based on multiple second level labels in pandas data frame

I would like to select columns in a pandas data frame that was constructed with a multi-index. In particular, I would like to select the columns based on more than one second-level label. An example:

In the following pandas data frame:

           bar       bar       baz       baz       foo       foo       qux  
           one       two       three     two       one       three    one   
A       0.895717  0.805244 -1.206412  2.565646  1.431256  1.340309 -1.170299   
B       0.410835  0.813850  0.132003 -0.827317 -0.076467 -1.187678  1.130127   
C      -1.413681  1.607920  1.024180  0.569605  0.875906 -2.211372  0.974466   

How can I select all columns on the second level that with the label "three" or "two" no matter what the first level labels are? Please keep in mind that this is just an example data frame, so just selecting all labels that are not "one" is not an option for me.

I tried

df_b = df.xs(['two','three'],level='second') 

or small variants thereof without success. Any help is appreciated. Thanks!

Upvotes: 3

Views: 1567

Answers (1)

keg5038
keg5038

Reputation: 341

You can use some combination of .loc. In your example, you could do this with the following code:

idx = pd.IndexSlice
df.loc[idx[:],idx[:,['two','three']]]

This should return what you're looking for. Does that work?

Upvotes: 6

Related Questions