0

I'm trying to remove a list of columns from a pandas.DataFrame. I've created a list through

diff = df1.difference(df2)

I would like to do something like:

for name in diff:
    dataframe.pop(name)

Is there a way to apply the pop function in a vectorized way with all names in the diff index array?

Thanks all for the help!

Regards, Jan

Jan Jansen
  • 53
  • 5

1 Answers1

1

as MaxU said, cleanest way to do it is

dataframe.drop(diff, axis=1)

the .pop() method returns the series you are poping, which is not time effective if you only want to delete the column

also, you coulduse the del method which maps to the python magic method df.__delitem__('column'). I would not recommend that.

you can read this great SO answer to learn more about those

Community
  • 1
  • 1
Steven G
  • 16,244
  • 8
  • 53
  • 77