[Python] Index Pandas with multiple boolean arrays

Discussão em 'Python' iniciado por Stack, Março 10, 2025.

  1. Stack

    Stack Membro Participativo

    Using numpy, one can subset an array with one boolean array per dimension like:

    In [10]: aa = np.array(range(9)).reshape(-1, 3)

    In [11]: aa
    Out[11]:
    array([[0, 1, 2],
    [3, 4, 5],
    [6, 7, 8]])

    In [12]: conditions = (np.array([True, True, False]), np.array([True, False, True]))

    In [13]: aa[np.ix_(*conditions)]
    Out[13]:
    array([[0, 2],
    [3, 5]])


    Is there a way to do this in Pandas? I've looked in their docs
    https://pandas.pydata.org/docs/user_guide/indexing.html#boolean-indexing
    but didn't find it. (I would have posted 4 relevant links, but then the automatic question checks think I've posted code that is not properly formatted.)

    This
    https://github.com/pandas-dev/pandas/issues/11290
    github issue is close, but I want to pick entire rows and columns.

    Continue reading...

Compartilhe esta Página