Skip to content
Advertisement

How to get rid of “Unnamed: 0” column in a pandas DataFrame read in from CSV file?

I have a situation wherein sometimes when I read a csv from df I get an unwanted index-like column named unnamed:0.

file.csv

JavaScript

The CSV is read with this:

JavaScript

This is very annoying! Does anyone have an idea on how to get rid of this?

Advertisement

Answer

It’s the index column, pass pd.to_csv(..., index=False) to not write out an unnamed index column in the first place, see the to_csv() docs.

Example:

JavaScript

compare with:

JavaScript

You could also optionally tell read_csv that the first column is the index column by passing index_col=0:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement