4

I want to drop multiple columns from pandas.DataFrame and can't do it.

In [10]: object_columns = X.select_dtypes(['object']).columns

In [10]: type(object_columns)

Out[10]: pandas.core.index.Index

In [11]: X = X.drop(object_columns, inplace=True, axis=1)

ValueError: labels ['VAR_0001' 'VAR_0005' 'VAR_0044' 'VAR_0073'] not contained in axis

I change code and it doesn't help:

In [12]: X = X.drop(X[object_columns], inplace=True, axis=1)

KeyError: "['VAR_0001' 'VAR_0005' 'VAR_0044' 'VAR_0073'] not in index"

Can anybody explain what I doing wrong?

Gilaztdinov Rustam
  • 2,281
  • 5
  • 18
  • 22
  • Maybe try dropping by column numbers such as df.drop(df.columns[[0,1,3]], axis=1, inplace=True) as shown at http://stackoverflow.com/questions/13411544/delete-column-from-pandas-dataframe - if only to see if that works. –  Sep 01 '15 at 09:13

3 Answers3

6

You need to do X.drop(object_columns, inplace=True, axis=1)

by passing inplace=True it operates on the object and returns nothing so you shouldn't be assigning it back, see the docs.

So you don't need:

X = X.drop(object_columns, inplace=True, axis=1)

just

X.drop(object_columns, inplace=True, axis=1)

if you passed inplace=False which is the default:

X = X.drop(object_columns, inplace=False, axis=1)

then it would have worked

EdChum
  • 376,765
  • 198
  • 813
  • 562
  • I don't understand what you mean – Gilaztdinov Rustam Sep 01 '15 at 09:10
  • You passed `inplace=True` this means it doesn't return a copy of the modified df, rather it operates on the original df and returns nothing so you don't need `X = X.drop(object_columns, inplace=True, axis=1)`, just `X.drop(object_columns, inplace=True, axis=1)` – EdChum Sep 01 '15 at 09:13
4

You can try the following

If you want to remove "Index" column use

X.reset_index()

here removed index column will be added as a new column.

If you want to just delete index columns use

X.reset_index(drop=True)

If you just want to drop some columns but not only one use the following

X.drop(['list of column names, seperated by comma'], inplace=True, axis=1)

Okroshiashvili
  • 3,677
  • 2
  • 26
  • 40
0

Use level in pandas.DataFrame.reset_index() to drop specific index column.

Jiaxiang
  • 865
  • 12
  • 23