[Python] quickly drop dataframe columns with only one distinct value

Discussão em 'Python' iniciado por Stack, Junho 23, 2025.

  1. Stack

    Stack Membro Participativo

    Is there a faster way to drop columns that only contain one distinct value than the code below?

    cols=df.columns.tolist()
    for col in cols:
    if len(set(df[col].tolist()))<2:
    df=df.drop(col, axis=1)


    This is really quite slow for large dataframes. Logically, this counts the number of values in each column when in fact it could just stop counting after reaching 2 different values.

    Continue reading...

Compartilhe esta Página