Reputation: 117380
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 use del
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?
Upvotes: 7
Views: 19073
Reputation: 1702
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.
Upvotes: 6