Pandas - Delete Rows with only NaN values

前端 未结 2 968
日久生厌
日久生厌 2021-02-05 13:54

I have a DataFrame containing many NaN values. I want to delete rows that contain too many NaN values; specifically: 7 or more.

I tried using the dr

相关标签:
2条回答
  • 2021-02-05 14:08

    Basically the way to do this is determine the number of cols, set the minimum number of non-nan values and drop the rows that don't meet this criteria:

    df.dropna(thresh=(len(df) - 7))
    

    See the docs

    0 讨论(0)
  • 2021-02-05 14:13

    The optional thresh argument of df.dropna lets you give it the minimum number of non-NA values in order to keep the row.

    df.dropna(thresh=df.shape[1]-7)
    
    0 讨论(0)
提交回复
热议问题