Home > front end >  pandas efficient way to drop columns in for-loop
pandas efficient way to drop columns in for-loop

Time:09-17

I have a list of columns that I want to drop from a dataframe. I don't know if all the columns are present across multiple dataframes, which is why I want to iterate over them and drop the ones that are present. For example:

cols = ['one', 'two', 'three']
for col in cols:
    try:
        df = df.drop(col, axis=1)
    except:
        pass

Is there a more efficient way to do this that saves time/processing power?

CodePudding user response:

You can set errors='ignore':

df = df.drop(cols, axis=1, errors='ignore')

Alternatively, you can use an index intersection:

cols = ['one', 'two', 'three']

df = df.drop(df.columns.intersection(cols), axis=1)

Or difference:

df = df[df.columns.difference(cols)]
  • Related