You can use the following syntax to drop one column from a pandas DataFrame by index number:
#drop first column from DataFrame df.drop(df.columns[0], axis=1, inplace=True)
And you can use the following syntax to drop multiple columns from a pandas DataFrame by index numbers:
#drop first, second, and fourth column from DataFrame cols = [0, 1, 3] df.drop(df.columns[cols], axis=1, inplace=True)
If your DataFrame has duplicate column names, you can use the following syntax to drop a column by index number:
#define list of columns cols = [x for x in range(df.shape[1])] #drop second column cols.remove(1) #view resulting DataFrame df.iloc[:, cols]
The following examples show how to drop columns by index in practice.
Example 1: Drop One Column by Index
The following code shows how to drop the first column in a pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['Mavs', 'Lakers', 'Spurs', 'Cavs'], 'first': ['Dirk', 'Kobe', 'Tim', 'Lebron'], 'last': ['Nowitzki', 'Bryant', 'Duncan', 'James'], 'points': [26, 31, 22, 29]}) #drop first column from DataFrame df.drop(df.columns[0], axis=1, inplace=True) #view resulting dataFrame df first last points 0 Dirk Nowitzki 26 1 Kobe Bryant 31 2 Tim Duncan 22 3 Lebron James 29
Example 2: Drop Multiple Columns by Index
The following code shows how to drop multiple columns in a pandas DataFrame by index:
import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['Mavs', 'Lakers', 'Spurs', 'Cavs'], 'first': ['Dirk', 'Kobe', 'Tim', 'Lebron'], 'last': ['Nowitzki', 'Bryant', 'Duncan', 'James'], 'points': [26, 31, 22, 29]}) #drop first, second and fourth columns from DataFrame cols = [0, 1, 3] df.drop(df.columns[cols], axis=1, inplace=True) #view resulting dataFrame df last 0 Nowitzki 1 Bryant 2 Duncan 3 James
Example 3: Drop One Column by Index with Duplicates
The following code shows how to drop a column by index number in a pandas DataFrame when duplicate column names exist:
import pandas as pd
#create DataFrame
df = pd.DataFrame({'team': ['Mavs', 'Lakers', 'Spurs', 'Cavs'],
'last': ['Nowitzki', 'Bryant', 'Duncan', 'James'],
'last': ['Nowitzki', 'Bryant', 'Duncan', 'James'],
'points': [26, 31, 22, 29]},
columns=['team', 'last', 'last', 'points'])
#define list of columns range
cols = [x for x in range(df.shape[1])]
#remove second column in DataFrame
cols.remove(1)
#view resulting DataFrame
df.iloc[:, cols]
team last points
0 Mavs Nowitzki 26
1 Lakers Bryant 31
2 Spurs Duncan 22
3 Cavs James 29
Additional Resources
How to Combine Two Columns in Pandas
Pandas: How to Sort Columns by Name
Pandas: How to Find the Difference Between Two Columns
Pandas: How to Sum Columns Based on a Condition