How to Drop First Column in Pandas DataFrame (3 Methods)


You can use one of the following methods to drop the first column in a pandas DataFrame:

Method 1: Use drop

df.drop(columns=df.columns[0], axis=1, inplace=True)

Method 2: Use iloc

df = df.iloc[: , 1:]

Method 3: Use del

del df[df.columns[0]]

Bonus Method 4: Use pop

first_col = df.pop(df.columns[0])

Bonus Method 5: Use Index slicing

df = df.loc[:, df.columns[1:]]

Each method produces the same result.

The following examples show how to use each method in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
                   'position': ['G', 'G', 'F', 'F', 'G', 'G', 'F', 'F'],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
df

	team	position assists rebounds
0	A	G	 5	 11
1	A	G	 7	 8
2	A	F	 7	 10
3	A	F	 9	 6
4	B	G	 12	 6
5	B	G	 9	 5
6	B	F	 9	 9
7	B	F	 4	 12

Method 1: Use drop

The following code shows how to use the drop() function to drop the first column of the pandas DataFrame:

#drop first column of DataFrame
df.drop(columns=df.columns[0], axis=1, inplace=True)

#view updated DataFrame
df

	position assists rebounds
0	G	 5	 11
1	G	 7	 8
2	F	 7	 10
3	F	 9	 6
4	G	 12	 6
5	G	 9	 5
6	F	 9	 9
7	F	 4	 12

Notice that the first column called ‘team’ has been removed from the DataFrame.

Also note that we must use inplace=True for the column to be removed in the original DataFrame.

Method 2: Use iloc

The following code shows how to use the iloc function to drop the first column of the pandas DataFrame:

#drop first column of DataFrame
df = df.iloc[: , 1:]

#view updated DataFrame
df

	position assists rebounds
0	G	 5	 11
1	G	 7	 8
2	F	 7	 10
3	F	 9	 6
4	G	 12	 6
5	G	 9	 5
6	F	 9	 9
7	F	 4	 12

Notice that the first column called ‘team’ has been removed from the DataFrame.

Method 3: Use del

The following code shows how to use the del function to drop the first column of the pandas DataFrame:

#drop first column of DataFrame
del df[df.columns[0]]

#view updated DataFrame
df

	position assists rebounds
0	G	 5	 11
1	G	 7	 8
2	F	 7	 10
3	F	 9	 6
4	G	 12	 6
5	G	 9	 5
6	F	 9	 9
7	F	 4	 12

Notice that the first column called ‘team’ has been removed from the DataFrame.

Bonus Method 4: Use pop

The following code shows how to use the pop() function to drop the first column of the pandas DataFrame:

#drop first column of DataFrame
first_col = df.pop(df.columns[0])

#view updated DataFrame
df

	position assists rebounds
0	G	 5	 11
1	G	 7	 8
2	F	 7	 10
3	F	 9	 6
4	G	 12	 6
5	G	 9	 5
6	F	 9	 9
7	F	 4	 12

Notice that the first column called ‘team’ has been removed from the DataFrame.

The pop() method is useful when you want to remove the first column and use it separately, as it returns the removed column as a Series.

Bonus Method 5: Use Index slicing

The following code shows how to use loc with index slicing to drop the first column of the pandas DataFrame:

#drop first column of DataFrame
df = df.loc[:, df.columns[1:]]

#view updated DataFrame
df

	position assists rebounds
0	G	 5	 11
1	G	 7	 8
2	F	 7	 10
3	F	 9	 6
4	G	 12	 6
5	G	 9	 5
6	F	 9	 9
7	F	 4	 12

Notice that the first column called ‘team’ has been removed from the DataFrame.

This method uses explicit index slicing with the loc accessor, which can be useful when working with specific column ranges.

Wrapping Up

We’ve explored five different ways to remove the first column from a pandas DataFrame. Each approach has specific benefits depending on your needs:

  • drop(): Suitable when you need fine control over axis and inplace parameters
  • iloc: Simple positional indexing when you know the column position
  • del: Quick and straightforward with minimal syntax
  • pop(): Beneficial when you need to retain the removed column for later use
  • loc: Helpful when working with named indices or specific column selections

For most standard operations, any of these methods will work effectively. Choose the one that best fits your coding style and specific requirements.

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

How to Drop Duplicate Columns in Pandas
How to Drop Rows by Index in Pandas
How to Drop Columns by Index in Pandas
How to Drop Rows that Contain Specific Value in Pandas

Leave a Reply

Your email address will not be published. Required fields are marked *