14

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!

Cranjis
  • 1,590
  • 8
  • 31
  • 64
  • 1
    Please provide the first 2-3 lines (including the headers) of the CSV file and the expected dataframe (if you can't share the actual data then dummy data in the same format is sufficient) – DeepSpace Dec 31 '18 at 13:52
  • 2
    While you should really provide sample data, Im guessing what you want is to add `index=False` to `pd.to_csv` – Jondiedoop Dec 31 '18 at 13:55
  • @okuoub, can you provide the initial lines of the code . – Karn Kumar Dec 31 '18 at 14:35
  • @okuoub , see the answer , mostly that should cover your problem, if that helps then you can upvote or even accept an an answer. – Karn Kumar Dec 31 '18 at 15:07

1 Answers1

20

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.

Karn Kumar
  • 8,518
  • 3
  • 27
  • 53