[Python] Conditionally slice a pandas multiindex on specific level

Discussão em 'Python' iniciado por Stack, Outubro 1, 2024.

  1. Stack

    Stack Membro Participativo

    For my given multi-indexed DataFrame:

    df = pd.DataFrame(
    np.random.randn(12),
    index=[
    [1,1,2,3,4,4,5,5,6,6,7,8],
    [1,2,1,1,1,2,1,2,1,2,2,2],
    ]
    )


    0
    1 1 1.667692
    2 0.274428
    2 1 0.216911
    3 1 -0.513463
    4 1 -0.642277
    2 -2.563876
    5 1 2.301943
    2 1.455494
    6 1 -1.539390
    2 -1.344079
    7 2 0.300735
    8 2 0.089269


    I would like to slice it such that I keep only rows where second index level contains BOTH 1 and 2

    0
    1 1 1.667692
    2 0.274428
    4 1 -0.642277
    2 -2.563876
    5 1 2.301943
    2 1.455494
    6 1 -1.539390
    2 -1.344079


    How can I do this?

    Continue reading...

Compartilhe esta Página