how to remove quasi constant column in pandas dataframe 1

how to remove quasi constant column in pandas dataframe

def drop_constant_column(dataframe):
    """
    Drops constant value columns of pandas dataframe.
    """
    return dataframe.loc[:, (dataframe != dataframe.iloc[0]).any()]

Here is what the above code is Doing:
1. dataframe.iloc[0] returns the first row of the dataframe.
2. dataframe != dataframe.iloc[0] returns a dataframe of booleans, where each cell is True if the value is different from the first row, and False if it’s the same.
3. dataframe.any() returns a series of booleans, where each cell is True if any of the values in the column are True, and False if all of the values are False.
4. dataframe.loc[:, (dataframe != dataframe.iloc[0]).any()] returns a dataframe with only the columns where any of the values are different from the first row.

Similar Posts