How to Drop Unnamed Column in Pandas DataFrame


You can use the following two methods to drop a column in a pandas DataFrame that contains “Unnamed” in the column name:

Method 1: Drop Unnamed Column When Importing Data

df = pd.read_csv('my_data.csv', index_col=0)

Method 2: Drop Unnamed Column After Importing Data

df = df.loc[:, ~df.columns.str.contains('^Unnamed')]

The following examples show how to use each method in practice.

Example 1: Drop Unnamed Column When Importing Data

Suppose we create a simple pandas DataFrame and export it to a CSV file:

import pandas as pd

#create DataFrame
df1 = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F'],
                    'points': [4, 4, 6, 8, 9, 5],
                    'rebounds': [12, 7, 8, 8, 5, 11]})

#view DataFrame
print(df1)

  team  points  rebounds
0    A       4        12
1    B       4         7
2    C       6         8
3    D       8         8
4    E       9         5
5    F       5        11

#export DataFrame to CSV file
df1.to_csv('my_data.csv')

Now when we attempt to read the file into a pandas DataFrame, the first column has a name of Unnamed: 0

#import CSV file
df2 = pd.read_csv('my_data.csv')

#view DataFrame
print(df2)

   Unnamed: 0 team  points  rebounds
0           0    A       4        12
1           1    B       4         7
2           2    C       6         8
3           3    D       8         8
4           4    E       9         5
5           5    F       5        11

To avoid this, we can specify index_col=0 to tell pandas that the first column is actually the index column:

#import CSV file
df2 = pd.read_csv('my_data.csv', index_col=0)

#view DataFrame
print(df2)

  team  points  rebounds
0    A       4        12
1    B       4         7
2    C       6         8
3    D       8         8
4    E       9         5
5    F       5        11

Example 2: Drop Unnamed Column After Importing Data

Suppose we create a simple pandas DataFrame and export it to a CSV file:

import pandas as pd

#create DataFrame
df1 = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F'],
                    'points': [4, 4, 6, 8, 9, 5],
                    'rebounds': [12, 7, 8, 8, 5, 11]})

#export DataFrame to CSV file
df1.to_csv('my_data.csv')

Now suppose we import this file into a pandas DataFrame:

#import CSV file
df2 = pd.read_csv('my_data.csv')

#view DataFrame
print(df2)

   Unnamed: 0 team  points  rebounds
0           0    A       4        12
1           1    B       4         7
2           2    C       6         8
3           3    D       8         8
4           4    E       9         5
5           5    F       5        11

To drop the column that contains “Unnamed” in the name, we can use the following syntax:

#drop any column that contains "Unnamed" in column name
df2 = df2.loc[:, ~df2.columns.str.contains('^Unnamed')]

#view updated DataFrame
print(df2)

  team  points  rebounds
0    A       4        12
1    B       4         7
2    C       6         8
3    D       8         8
4    E       9         5
5    F       5        11

Notice that the “Unnamed: 0” column has been dropped from the DataFrame.

Additional Resources

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

How to Drop First Row in Pandas DataFrame
How to Drop First Column in Pandas DataFrame
How to Drop Duplicate Columns in Pandas

Leave a Reply

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