Drop Unnamed: 0 columns from a Pandas DataFrame in Python

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
6 min

banner

# Table of Contents

  1. Drop Unnamed: 0 columns from a Pandas DataFrame in Python
  2. Removing Unnamed: 0 columns by explicitly setting the index column
  3. Removing Unnamed: 0 columns by using str.match()
  4. Using df.drop() to drop the Unnamed columns
  5. Drop Unnamed:0 columns by renaming them

# Drop Unnamed: 0 columns from a Pandas DataFrame in Python

You will most commonly get Unnamed: 0 columns in a Pandas DataFrame when it is saved with an index in a CSV file.

To resolve the issue, set the index argument to False when saving the DataFrame to a CSV file.

For example, running the following code sample produces an Unnamed: 0 column.

main.py
import pandas as pd df = pd.DataFrame( [[1, 2, 3], [4, 5, 6], [7, 8, 8]], columns=['bobby', 'hadz', 'com'] ) print(df) df.to_csv('data.csv', encoding='utf-8') df = pd.read_csv('data.csv', sep=',', encoding='utf-8') print('-' * 50) print(df)
The code for this article is available on GitHub

Here is the output of running the code sample:

shell
bobby hadz com 0 1 2 3 1 4 5 6 2 7 8 8 -------------------------------------------------- Unnamed: 0 bobby hadz com 0 0 1 2 3 1 1 4 5 6 2 2 7 8 8

drop unnamed 0 columns from pandas dataframe

Notice that we have an Unnamed: 0 column.

This is caused by saving our CSV file with an unnamed index.

You can resolve the issue by setting the index argument to False when calling DataFrame.to_csv.

main.py
import pandas as pd df = pd.DataFrame( [[1, 2, 3], [4, 5, 6], [7, 8, 8]], columns=['bobby', 'hadz', 'com'] ) print(df) # ✅ Set index to False df.to_csv('data.csv', encoding='utf-8', index=False) df = pd.read_csv('data.csv', sep=',', encoding='utf-8') print('-' * 50) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
bobby hadz com 0 1 2 3 1 4 5 6 2 7 8 8 -------------------------------------------------- bobby hadz com 0 1 2 3 1 4 5 6 2 7 8 8

set index argument to false when calling to csv

Notice that we don't have an Unnamed: 0 column anymore.

The index argument defaults to True.

When the argument is set to False, the row names (indices) are not written.

Note: the issue also occurs when you end each row with a comma when writing your data to a CSV file. Make sure you don't have any trailing commas.

# Removing Unnamed: 0 columns by explicitly setting the index column

You can also explicitly set the index_col argument in the call to pandas.read_csv to resolve the issue.

main.py
import pandas as pd df = pd.DataFrame( [[1, 2, 3], [4, 5, 6], [7, 8, 8]], columns=['bobby', 'hadz', 'com'] ) print(df) df.to_csv('data.csv', encoding='utf-8') # ✅ Explicitly set index_col to 0 df = pd.read_csv( 'data.csv', sep=',', encoding='utf-8', index_col=[0] ) print('-' * 50) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
bobby hadz com 0 1 2 3 1 4 5 6 2 7 8 8 -------------------------------------------------- bobby hadz com 0 1 2 3 1 4 5 6 2 7 8 8

explicitly setting index col argument to resolve the issue

The index_col argument determines the column that is used as the row label of the DataFrame.

We used a column index of 0 to set the first column as the index.

main.py
# ✅ explicitly set index_col to 0 df = pd.read_csv( 'data.csv', sep=',', encoding='utf-8', index_col=[0] )

This should be your preferred approach when you don't have access to the code that saves the DataFrame to a CSV file.

# Removing Unnamed: 0 columns by using str.match()

You can also use the str.match() method to drop the Unnamed columns from your DataFrame.

The method enables us to determine whether each string starts with a match of a regular expression.

main.py
import pandas as pd df = pd.DataFrame( [[1, 2, 3], [4, 5, 6], [7, 8, 8]], columns=['bobby', 'hadz', 'com'] ) print(df) df.to_csv('data.csv', encoding='utf-8') df = pd.read_csv( 'data.csv', sep=',', encoding='utf-8', ) print('-' * 50) print(df) # 👇️ Index(['Unnamed: 0', 'bobby', 'hadz', 'com'], dtype='object') print(df.columns) print('-' * 50) df = df.loc[:, ~df.columns.str.match('Unnamed')] print(df) # 👇️ Index(['bobby', 'hadz', 'com'], dtype='object') print(df.columns)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
bobby hadz com 0 1 2 3 1 4 5 6 2 7 8 8 -------------------------------------------------- Unnamed: 0 bobby hadz com 0 0 1 2 3 1 1 4 5 6 2 2 7 8 8 Index(['Unnamed: 0', 'bobby', 'hadz', 'com'], dtype='object') -------------------------------------------------- bobby hadz com 0 1 2 3 1 4 5 6 2 7 8 8 Index(['bobby', 'hadz', 'com'], dtype='object')

We used the str.match() method to match all columns that start with the string Unnamed.

The matching columns are then dropped using the DataFrame.loc label indexer.

# Using df.drop() to drop the Unnamed columns

You can also use the DataFrame.drop and DataFrame.filter method to drop the Unnamed columns from your DataFrame.

main.py
import pandas as pd df = pd.DataFrame( [[1, 2, 3], [4, 5, 6], [7, 8, 8]], columns=['bobby', 'hadz', 'com'] ) print(df) df.to_csv('data.csv', encoding='utf-8') df = pd.read_csv( 'data.csv', sep=',', encoding='utf-8', ) print('-' * 50) print(df) # 👇️ Index(['Unnamed: 0', 'bobby', 'hadz', 'com'], dtype='object') print(df.columns) print('-' * 50) df.drop(df.filter(regex="Unname"), axis=1, inplace=True) print(df) # 👇️ Index(['bobby', 'hadz', 'com'], dtype='object') print(df.columns)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
bobby hadz com 0 1 2 3 1 4 5 6 2 7 8 8 -------------------------------------------------- Unnamed: 0 bobby hadz com 0 0 1 2 3 1 1 4 5 6 2 2 7 8 8 Index(['Unnamed: 0', 'bobby', 'hadz', 'com'], dtype='object') -------------------------------------------------- bobby hadz com 0 1 2 3 1 4 5 6 2 7 8 8 Index(['bobby', 'hadz', 'com'], dtype='object')

We used the DataFrame.filter() method to get the DataFrame columns that start with Unname.

The next step is to use the DataFrame.drop() method to drop the matching columns in place.

When the inplace argument is set to True, the original DataFrame is updated, so no reassignment is necessary.

# Drop Unnamed:0 columns by renaming them

Alternatively, you can rename the Unnamed: 0 columns by using the DataFrame.rename method.

main.py
import pandas as pd df = pd.DataFrame( [[1, 2, 3], [4, 5, 6], [7, 8, 8]], columns=['bobby', 'hadz', 'com'] ) print(df) df.to_csv('data.csv', encoding='utf-8') df = pd.read_csv( 'data.csv', sep=',', encoding='utf-8', ) print('-' * 50) print(df) # 👇️ Index(['Unnamed: 0', 'bobby', 'hadz', 'com'], dtype='object') print(df.columns) print('-' * 50) df.rename(columns={'Unnamed: 0': 'Example_Name'}, inplace=True) print(df) # 👇️ Index(['Example_Name', 'bobby', 'hadz', 'com'], dtype='object') print(df.columns)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
bobby hadz com 0 1 2 3 1 4 5 6 2 7 8 8 -------------------------------------------------- Unnamed: 0 bobby hadz com 0 0 1 2 3 1 1 4 5 6 2 2 7 8 8 Index(['Unnamed: 0', 'bobby', 'hadz', 'com'], dtype='object') -------------------------------------------------- Example_Name bobby hadz com 0 0 1 2 3 1 1 4 5 6 2 2 7 8 8 Index(['Example_Name', 'bobby', 'hadz', 'com'], dtype='object')

The DataFrame.rename() method renames columns or index labels.

main.py
df.rename( columns={'Unnamed: 0': 'Example_Name'}, inplace=True )

When the inplace argument is set to True, the column is renamed in the original DataFrame.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.