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
Hey there. My name is Zach Bobbitt. I have a Masters of Science degree in Applied Statistics and I’ve worked on machine learning algorithms for professional businesses in both healthcare and retail. I’m passionate about statistics, machine learning, and data visualization and I created Statology to be a resource for both students and teachers alike. My goal with this site is to help you learn statistics through using simple terms, plenty of real-world examples, and helpful illustrations.