Drop columns of DataFrames from a list of DataFrames using List Comprehension (Pandas)

Viewed 2791

I have a list of DataFrames that have the same columns and different values. I want to drop some columns from the list of DataFrames in one line in pandas.

For far, I tried (dfs has list of Data Frames)

dfs.drop([col for col in ['var1', 'var2'] if col in dfs], axis=1, inplace=True)

and

dfs[dfs.drop([col for col in ['var1', 'var2'] if col in dfs], axis=1, inplace=True)]

both are giving same error:

AttributeError: 'list' object has no attribute 'drop'

type(dfs)
>> list

However, when i can loop through each DataFRame from the list dfs using for loop, I can drop the columns.

How can I do it in the list comprehension way in pandas?

1 Answers
Related