pandas dataframe drop columns by number of nan

后端 未结 5 794
忘了有多久
忘了有多久 2021-01-04 02:08

I have a dataframe with some columns containing nan. I\'d like to drop those columns with certain number of nan. For example, in the following code, I\'d like to drop any co

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-04 02:27

    Here is a possible solution:

    s = dff.isnull().apply(sum, axis=0) # count the number of nan in each column
    print s
       A    1 
       B    1
       C    3
       dtype: int64
    
    for col in dff: 
       if s[col] >= 2:  
           del dff[col]
    

    Or

    for c in dff:
        if sum(dff[c].isnull()) >= 2:
            dff.drop(c, axis=1, inplace=True)
    

提交回复
热议问题