Don't use ix
. It's deprecated. The most readable and idiomatic way of doing this is df.drop()
:
>>> df.drop('b', axis=1)
a c d
0 0.418762 0.869203 0.972314
1 0.991058 0.594784 0.534366
2 0.407472 0.396664 0.894202
3 0.726168 0.324932 0.906575
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
. You can use columns
too, as in df.drop(columns='a')
or df.drop(columns=['a', 'b'])
(thanks @BallpointBen in the comments).