Return dataframe subset based on a list of boolean values

前端 未结 6 2245
星月不相逢
星月不相逢 2021-02-13 04:38

I\'m trying to slice a dataframe based on list of values, how would I go about this?

Say I have an expression or a list l = [0,1,0,0,1,1,0,0,0,1]

Ho

6条回答
  •  孤独总比滥情好
    2021-02-13 05:16

    yet another "creative" approach:

    In [181]: a = np.array(lst)
    
    In [182]: df.query("index * @a > 0")
    Out[182]:
       0  1  2
    1  1  5  5
    4  0  2  0
    5  4  9  9
    9  2  2  5
    

    or much better variant from @ayhan:

    In [183]: df.query("@a != 0")
    Out[183]:
       0  1  2
    1  1  5  5
    4  0  2  0
    5  4  9  9
    9  2  2  5
    

    PS i've also borrowed @Ayhan's setup

提交回复
热议问题