3

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:

user47
  • 1,080
  • 11
  • 28
Luyolo
  • 41
  • 1
  • 3
  • You might have a (or more) whitespace character in your column name of your dataframe. Tell us the exact output of `df.columns`. Otherwise, you might have multi-index columns. In that case, tell us what `df.info()` shows. – user47 Aug 27 '22 at 08:46
  • @Firelord, I thought when I put the command leaving space before SG and also attempted to put it after SG df = df.copy() df.drop(['SG '], axis = 1) AND df = df.copy() df.drop([' SG'], axis = 1) that would help with white space character, clearly it's not helping. You gave me two things to check, df.columns & df.info(), after trying these, I realized that I only get this error when I run the cell twice. I I click run once, "SG" column gets removed but as soon as I click again, the error comes, I guess at this stage it's just telling me indeed "SG" is not there. – Luyolo Aug 27 '22 at 10:57

1 Answers1

1

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.

Roy
  • 398
  • 2
  • 12
  • 1
    The problem doesn't seem to be this. OP reported this error. `KeyError: "['SG'] not found in axis"`. Whether you do drop inplace or not should not report this error unless the column doesn't really exist. – user47 Aug 27 '22 at 08:43
  • How do you construct your dataframe? – Roy Aug 27 '22 at 08:45
  • I'm not OP. That question should be addressed to OP. :) – user47 Aug 27 '22 at 08:45
  • ahh alright then. – Roy Aug 27 '22 at 08:47
  • I assume the DataFrame is probably incorrect in some way or the axis is just 0 and not 1. I tried it myself works just fine. – Roy Aug 27 '22 at 08:48
  • I have asked OP for some clarification in the comments. Hopefully, that should sort things out. – user47 Aug 27 '22 at 08:50
  • @Roy, thanks for the suggestion, there's definitely something wrong I'm doing here because, after seemingly resolved the issue of dropping "SG", when I put the dependent/ independent variables, I now get a new error as below: df.columns Index(['R', 'pH'], dtype='object') n=6 x = df.drop("pH") y = df.drop("pH",1) df.head(n=6) then the error comes as: KeyError: "['pH'] not found in axis" – Luyolo Aug 27 '22 at 11:03