python - Best way to eliminate columns with only one value from pandas dataframe -
i'm trying build function eliminate dataset columns 1 value. used function:
def onecatelimination(dataframe): columns=dataframe.columns.values column in columns: if len(dataframe[column].value_counts().unique())==1: del dataframe[column] return dataframe the problem function eliminates column more 1 distinct value, i.e. index column integer number..
just
df.dropna(thresh=2, axis=1) will work. no need else. keep columns 2 or more non-na values (controlled value passed thresh). axis kwarg let work rows or columns. rows default, need pass axis=1 explicitly work on columns (i forgot @ time answered, hence edit). see dropna() more information.
Comments
Post a Comment