Drop rows with all zeros in pandas data frame

前端 未结 13 1129
礼貌的吻别
礼貌的吻别 2020-11-27 11:57

I can use pandas dropna() functionality to remove rows with some or all columns set as NA\'s. Is there an equivalent function for drop

13条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 12:37

    It turns out this can be nicely expressed in a vectorized fashion:

    > df = pd.DataFrame({'a':[0,0,1,1], 'b':[0,1,0,1]})
    > df = df[(df.T != 0).any()]
    > df
       a  b
    1  0  1
    2  1  0
    3  1  1
    

提交回复
热议问题