How to select all columns, except one column in pandas?
Don't use ix
. It's deprecated. The most readable and idiomatic way of doing this is df.drop()
:
>>> df a b c d0 0.175127 0.191051 0.382122 0.8692421 0.414376 0.300502 0.554819 0.4975242 0.142878 0.406830 0.314240 0.0931323 0.337368 0.851783 0.933441 0.949598>>> df.drop('b', axis=1) a c d0 0.175127 0.382122 0.8692421 0.414376 0.554819 0.4975242 0.142878 0.314240 0.0931323 0.337368 0.933441 0.949598
Note that by default, .drop()
does not operate inplace; despite the ominous name, df
is unharmed by this process. If you want to permanently remove b
from df
, do df.drop('b', inplace=True)
.
df.drop()
also accepts a list of labels, e.g. df.drop(['a', 'b'], axis=1)
will drop column a
and b
.