Drop row in pandas dataframe if any value in the row equals zero

前端 未结 4 1132
粉色の甜心
粉色の甜心 2020-12-05 18:07

How do I drop a row if any of the values in the row equal zero?

I would normally use df.dropna() for NaN values but not sure how to do it with \"0\" values.

4条回答
  •  没有蜡笔的小新
    2020-12-05 19:03

    You could make a boolean frame and then use any:

    >>> df = pd.DataFrame([[1,0,2],[1,2,3],[0,1,2],[4,5,6]])
    >>> df
       0  1  2
    0  1  0  2
    1  1  2  3
    2  0  1  2
    3  4  5  6
    >>> df == 0
           0      1      2
    0  False   True  False
    1  False  False  False
    2   True  False  False
    3  False  False  False
    >>> df = df[~(df == 0).any(axis=1)]
    >>> df
       0  1  2
    1  1  2  3
    3  4  5  6
    

提交回复
热议问题