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!
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With