Remove list of columns from a DataFrame when some do not exist

Viewed 1584

For a pandas DataFrame, I can drop any existing columns using

data.drop([col], axis=1)

If I want to drop several columns, as long as they all exist in the data, I can drop them all at once using

data.drop(list_of_cols, axis=1)

How can I drop several columns if some do not exists. I want to make sure none of the columns in my list are in the data.

Is there a standard way to drop a list of columns ignoring those that do not exist instead of throwing KeyError.

2 Answers

As simple as

df.drop(columns=cols, errors='ignore')

The fastest method I found for removing several columns, when some are not in the DataFrame, is to use list comprehension.

col_exists = [col for col in list_of_cols if col in data.columns]
new_data = data.drop(col_exists, axis=1)

An alternative methods that was slightly slower (but mangled the column order) used set operations.

col_keep = set(data.columns) - set(list_of_cols)
new_data = data[col_keep]

And of course there is the slow loop option.

for col in list_of_cols:
    try:
        data = data.drop([col], axis=1)
    except KeyError:
        pass
Related