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?
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With