23.1 C
London
Monday, July 21, 2025
HomePandas in PythonDataFrame Functions in PythonHow to Drop Unnamed Column in Pandas DataFrame

How to Drop Unnamed Column in Pandas DataFrame

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories