Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas drop index type columns

Tags:

python

pandas

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?

like image 845
Gilaztdinov Rustam Avatar asked Jan 07 '23 15:01

Gilaztdinov Rustam


2 Answers

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

like image 129
EdChum Avatar answered Jan 18 '23 00:01

EdChum


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)

like image 43
Okroshiashvili Avatar answered Jan 17 '23 22:01

Okroshiashvili