718

I am trying to save a csv to a folder after making some edits to the file.

Every time I use pd.to_csv('C:/Path of file.csv') the csv file has a separate column of indexes. I want to avoid printing the index to csv.

I tried:

pd.read_csv('C:/Path to file to edit.csv', index_col = False)

And to save the file...

pd.to_csv('C:/Path to save edited file.csv', index_col = False)

However, I still got the unwanted index column. How can I avoid this when I save my files?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Alexis
  • 8,531
  • 5
  • 19
  • 21

6 Answers6

1085

Use index=False.

df.to_csv('your.csv', index=False)
ayhan
  • 70,170
  • 20
  • 182
  • 203
Probably rgbkrk
  • 10,906
  • 1
  • 11
  • 4
133

There are two ways to handle the situation where we do not want the index to be stored in csv file.

  1. As others have stated you can use index=False while saving your
    dataframe to csv file.

    df.to_csv('file_name.csv',index=False)

  2. Or you can save your dataframe as it is with an index, and while reading you just drop the column unnamed 0 containing your previous index.Simple!

    df.to_csv(' file_name.csv ')
    df_new = pd.read_csv('file_name.csv').drop(['unnamed 0'],axis=1)

blitu12345
  • 3,473
  • 1
  • 20
  • 21
  • 10
    "and while reading you just drop the column unnamed 0 containing your previous index" a better way do to this is specify `pd.read_csv(..., index_col=[0]`, and avoid the extra "drop" call. – cs95 May 28 '19 at 04:19
112

If you want no index, read file using:

import pandas as pd
df = pd.read_csv('file.csv', index_col=0)

save it using

df.to_csv('file.csv', index=False)
amalik2205
  • 3,962
  • 1
  • 15
  • 21
  • 7
    I cant believe nobody noticed the error. To save to csv, it would be `df.to_csv('file.csv', index=False)` – MEdwin Nov 13 '19 at 10:37
34

As others have stated, if you don't want to save the index column in the first place, you can use df.to_csv('processed.csv', index=False)

However, since the data you will usually use, have some sort of index themselves, let's say a 'timestamp' column, I would keep the index and load the data using it.

So, to save the indexed data, first set their index and then save the DataFrame:

df.set_index('timestamp')
df.to_csv('processed.csv')

Afterwards, you can either read the data with the index:

pd.read_csv('processed.csv', index_col='timestamp')

or read the data, and then set the index:

pd.read_csv('filename.csv')
pd.set_index('column_name')
Lucas P.
  • 4,282
  • 4
  • 29
  • 50
  • 1
    If I set the index_col then saved, I still had a numerical unnamed column in the csv. (Python2) – smiller Mar 21 '19 at 13:58
20

Another solution if you want to keep this column as index.

pd.read_csv('filename.csv', index_col='Unnamed: 0')
khaled Fouda
  • 359
  • 3
  • 6
  • 1
    Exactly what I was looking for, thank you. That somehow helps to translate the concept of primary key transparently, even when using csv – Tobbey Aug 21 '18 at 13:19
  • Very good idea!!! I tried it, and it's very elegant solution!!! – Dave Nov 14 '20 at 17:03
17

If you want a good format next statement is the best:

dataframe_prediction.to_csv('filename.csv', sep=',', encoding='utf-8', index=False)

In this case you have got a csv file with ',' as separate between columns and utf-8 format. In addition, numerical index won't appear.