Home > Software engineering >  getting KeyError while using .drop command
getting KeyError while using .drop command

Time:10-07

I know that I should add "axis=1" when I'm using df.drop command but after using it I still getting KeyError which says "['label'] not found in axis" Is there any other matters I should consider for that?

CodePudding user response:

I guess you don't have 'label' column in your dataframe.

I tested with sample example, I can trigger the same issue like you.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.arange(12).reshape(3, 4),
                  columns=['A', 'B', 'C', 'D'])

df.drop(['label'], axis=1)

Error:

KeyError: "['label'] not found in axis"

CodePudding user response:

In this case you should use this command '''column_headers = list(df.columns.values)''' to see your column headers and check the exact labels and if there is any space before or after the name.

  • Related