KeyError: "[Column] not found in axis"

14,079

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.

Share:
14,079
Branden
Author by

Branden

Updated on June 14, 2022

Comments

  • Branden
    Branden almost 2 years

    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"