Pandas remove column by index
18,487
df = df.drop(['b'], axis=1).join(df['b'].ix[:, 0:1])
>>> df
a b
0 1 2
1 4 5
2 7 8
Or just for this case
df = df.ix[:, 0:2]
But I think it has other better ways.

Author by
Roman Pekar
Constant battle for concise and elegant solutions. ormeugensson at gmail.com
Updated on June 13, 2022Comments
-
Roman Pekar about 1 year
Suppose I have a DataFrame like this:
>>> df = pd.DataFrame([[1,2,3], [4,5,6], [7,8,9]], columns=['a','b','b']) >>> df a b b 0 1 2 3 1 4 5 6 2 7 8 9
And I want to remove second
'b'
column. If I just usedel
statement, it'll delete both'b'
columns:>>> del df['b'] >>> df a 0 1 1 4 2 7
I can select column by index with
.iloc[]
and reassign DataFrame, but how can I delete only second'b'
column, for example by index?