Home > Net >  Python Pandas Error trying to drop the first column
Python Pandas Error trying to drop the first column

Time:12-27

I'm trying to drop the first column of a data frame, when I run

X.columns.tolist() 

I get this:

['colors', 'num_critic_for_reviews', 'duration', 'director_facebook_likes', 'actor_3_facebook_likes']

so, I want to drop 'colors', but when I run

X = X.drop('colors', index=1) I get:

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

I tried with the column index and also with the column label, but keep getting the same error. The funny thing is if I tried to access and use the column colors it works, but again if I try to drop it, get the Not found in the axis error.

CodePudding user response:

To drop the column you could use

X.drop(columns=['colors'])

If you want to explicitly remove the first one, just replace it inside the array:

X.drop(columns=[X.columns[0]])

CodePudding user response:

Use the axis=1 keyword instead of index=1

Check out the documentation

CodePudding user response:

Finally, it works if previously I reset the index of X and then use X.drop.

  • Related