I tried both:

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

AND

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

SG is the column on my data frame that I'm trying to drop.

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

This is what my data looks like, see on attached image

Image:

1

Best Answer


df.drop(['SG'], axis=1) doesn't change the dataframe inplace.

instead you need to override the DataFrame by doing this below.

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

Or by including the argument inplace=True Like this:

df.drop(['SG'], axis=1, inplace=True)

Either option should work fine.I'd recommend using the first option it will be more error prone for the future.