Return dataframe subset based on a list of boolean values

前端 未结 6 2231
星月不相逢
星月不相逢 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:14

    Convert the list to a boolean array and then use boolean indexing:

    df = pd.DataFrame(np.random.randint(10, size=(10, 3)))
    
    df[np.array(lst).astype(bool)]
    Out: 
       0  1  2
    1  8  6  3
    4  2  7  3
    5  7  2  3
    9  1  3  4
    

提交回复
热议问题