Skip to content

More tolerant dataframe drop method for multiple columns deletion #5300

Closed
@halleygithub

Description

@halleygithub

Current dataframe.drop will raise error for below code for 'non_exist_in_df_col' :

df = df.drop(['col_1', 'col_2', 'non_exist_in_df_col'], axis=1)

But below is better for it can accept it.

def drop_cols(df, del_cols):   
    for col in (set(del_cols) & set(df.columns)):
        df = df.drop([col], axis=1)
    return df
DataFrame.drop_cols = drop_cols

And it will be better to add an 'inplace' option to speed up the repeatly df self-assignment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    API DesignIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions