Remove constant columns with or without NAs

前端 未结 7 1515
深忆病人
深忆病人 2021-01-17 17:30

I am trying to get many lm models work in a function and I need to automatically drop constant columns from my data.table. Thus, I want to keep only columns wit

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-17 18:15

    For removing constant columns,

    1. Numeric Columns:-

      constant_col = [const for const in df.columns if df[const].std() == 0]
      print (len(constant_col))
      print (constant_col)
      
    2. Categorical Columns:-

      constant_col = [const for const in df.columns if len(df[const].unique()) == 1]
      print (len(constant_col))
      print (constant_col)
      

    Then you drop the columns using the drop method

提交回复
热议问题