Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

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!

like image 370
Eduard Geist Avatar asked Nov 24 '25 10:11

Eduard Geist


1 Answers

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?

like image 56
keg5038 Avatar answered Nov 26 '25 22:11

keg5038



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!