3

If a column exists between 2 dataframes, I want to drop it. I check if it exists and then try to drop it, but then it says that can't be found.

for column in positionsdf.columns:
    if column in entrydf.columns:
        entrydf = entrydf.drop(column)

error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-20-d9893719abf8> in <module>
      1 for column in positionsdf.columns:
      2     if column in entrydf.columns:
----> 3         entrydf = entrydf.drop(column)

...
...
...

KeyError: "['caseworker'] not found in axis"
Branden
  • 326
  • 1
  • 3
  • 14

1 Answers1

4

DataFrame#drop defaults to the row axis. You want to drop a column. Use syntax:

df.drop(columns=[columns, input, here])

You can also get rid of reassignment by passing parameter inplace=True.

ifly6
  • 5,003
  • 2
  • 24
  • 47