pythonpandasmulti-index

Discontinuous selections with pandas MultiIndex


I have the following DataFrame with MultiIndex columns (the same applies to MultiIndex rows):

    import pandas as pd
    df = pd.DataFrame(columns=pd.MultiIndex.from_product([['A','B'],[1,2,3,4]]),
             data=[[0,1,2,3,4,5,6,7],[10,11,12,13,14,15,16,17], 
                  [20,21,22,23,24,25,26,27]])

Now I want to select the following columns into a new DataFrame: from the A group, elements at index [1,2,4], AND from the B group, elements at index [1,3]. So my new DataFrame would have 5 columns.

I can easily make any of the two selections separately using .loc:

    df_grA = df.loc[:,('A',(1,2,4))]
    df_grB = df.loc[:,('B',(1,3))]

But I cannot find a way to achieve what I want. The only way that I can think of is to concat the two pieces together like that:

    df_selection = pd.concat([df_grA,df_grB],axis=1)

This works, but it's clunky. I can't believe there's not a more convenient way.


Solution

  • Another idea with product:

    from  itertools import  product
    
    print (df.loc[:, list(product('A',(1,2,4))) + list(product('B',(1,2,3)))])
        A           B        
        1   2   4   1   2   3
    0   0   1   3   4   5   6
    1  10  11  13  14  15  16
    2  20  21  23  24  25  26