pandas efficient way to drop columns in for-loop


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:
        df = df.drop(col, axis=1)

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


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)]
Answered By: mozway
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.