Pandas remove column by index

Suppose I have a DataFrame:

>>> df = pd.DataFrame([[1,2,3], [4,5,6], [7,8,9]], columns=['a','b','b']) >>> df abb 0 1 2 3 1 4 5 6 2 7 8 9 

And I want to remove the second column 'b' . If I just use the del operator, it will remove the 'b' columns:

 >>> del df['b'] >>> df a 0 1 1 4 2 7 

I can select the column by index with .iloc[] and reassign the DataFrame, but how to remove only the second column 'b' , for example, by index?

+7
python pandas
source share
1 answer
 df = df.drop(['b'], axis=1).join(df['b'].ix[:, 0:1]) >>> df ab 0 1 2 1 4 5 2 7 8 

Or just for this occasion

 df = df.ix[:, 0:2] 

But I think he has other better ways.

+5
source share

All Articles