[Python] MultiIndex Slice | slice with another index across a subset of levels

Discussão em 'Python' iniciado por Stack, Agosto 15, 2025.

  1. Stack

    Stack Membro Participativo

    I am wondering the best way to slice a multi-index, using another index, where the other index is a subset of the main multi-index.

    np.random.seed(1)
    dict_data_russian = {'alpha':[1,2,3,4,5,6,7,8,9],'beta':['a','b','c','d','e','f','g','h','i'],'gamma':['r','s','t','u','v','w','x','y','z'],'value_r': np.random.rand(9)}
    dict_data_doll = {'beta':['d','e','f'],'gamma':['u','v','w'],'dont_care': list('PQR')}
    df_russian = pd.DataFrame(data=dict_data_russian)
    df_russian.set_index(['alpha','beta','gamma'],inplace=True)
    df_doll = pd.DataFrame(data=dict_data_doll)
    df_doll.set_index(['beta','gamma'],inplace=True)

    print df_russian
    print df_doll.head()


    Which yields:

    value_r
    alpha beta gamma
    1 a r 0.4170
    2 b s 0.7203
    3 c t 0.0001
    4 d u 0.3023
    5 e v 0.1468
    6 f w 0.0923
    7 g x 0.1863
    8 h y 0.3456
    9 i z 0.3968


    dont_care
    beta gamma
    d u P
    e v Q
    f w R


    How best to use the index in df_doll to slice df_russian, on levels beta & gamma, in order to the following output?

    value_r
    alpha beta gamma
    4 d u 0.3023
    5 e v 0.1468
    6 f w 0.0923

    Continue reading...

Compartilhe esta Página