pd.read_csv add column named "Unnamed: 0

Viewed 11731

I have a dataframe with 3 columns. I save with pd.to_csv(filename) and then re-open it with

pd.read_csv(filename, index_col=False)

But I get a dataframe with 4 columns, with the left-most column called

Unnamed:0

that is actually just the row number. How can I read the csv without it?

Thanks!

1 Answers

You should try:

pd.read_csv('file.csv', index_col=0)

index_col : int or sequence or False, default None Column to use as the row labels of the DataFrame. If a sequence is given, a MultiIndex is used. If you have a malformed file with delimiters at the end of each line, you might consider index_col=False to force pandas to not use the first column as the index (row names)

Example Dataset:

I have taken the dataset from google,So while i'm simply trying to import the data with pd.read_csv it shows the Unnamed: 0 as default.

>>> df = pd.read_csv("amis.csv")
>>> df.head()
   Unnamed: 0  speed  period  warning  pair
0           1     26       1        1     1
1           2     26       1        1     1
2           3     26       1        1     1
3           4     26       1        1     1
4           5     27       1        1     1

So, Just to avoid the the Unnamed: 0 we have to use index_col=0 and will get the nicer dataframe:

>>> df = pd.read_csv("amis.csv", index_col=0)
>>> df.head()
   speed  period  warning  pair
1     26       1        1     1
2     26       1        1     1
3     26       1        1     1
4     26       1        1     1
5     27       1        1     1

Note : So, to make it more explicit to understand when we say index_col=0, it placed the first column as the index in the dataFrame rather appearing as Unnamed: 0 .

Hope this will help.

Related