4

I want to read from a CSV file using pandas read_csv. The CSV file doesn't have column names. When I use pandas to read the CSV file, the first row is set as columns by default. But when I use df.columns = ['ID', 'CODE'], the first row is gone. I want to add, not replace.

df = pd.read_csv(CSV)
df

    a   55000G707270
0   b   5l0000D35270
1   c   5l0000D63630
2   d   5l0000G45630
3   e   5l000G191200
4   f   55000G703240


df.columns=['ID','CODE']
df

    ID          CODE
0   b   5l0000D35270
1   c   5l0000D63630
2   d   5l0000G45630
3   e   5l000G191200
4   f   55000G703240
Julien Marrec
  • 11,605
  • 4
  • 46
  • 63
running man
  • 1,377
  • 4
  • 14
  • 21
  • Possible duplicate of [How to add header row to a pandas DataFrame](http://stackoverflow.com/questions/34091877/how-to-add-header-row-to-a-pandas-dataframe) – Leb Dec 24 '16 at 13:04

5 Answers5

10

I think you need parameter names in read_csv:

df = pd.read_csv(CSV, names=['ID','CODE'])

names : array-like, default None

List of column names to use. If file contains no header row, then you should explicitly pass header=None. Duplicates in this list are not allowed unless mangle_dupe_cols=True, which is the default.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

You may pass the column names at the time of reading the csv file itself as :

df = pd.read_csv(csv_path, names = ["ID", "CODE"])
ZdaR
  • 22,343
  • 7
  • 66
  • 87
1

Use names argument in function call to add the columns yourself:

df = pd.read_csv(CSV, names=['ID','CODE'])
Carles Mitjans
  • 4,786
  • 3
  • 19
  • 38
1

you need both: header=None and names=['ID','CODE'], because there are no column names/labels/headers in your CSV file:

df = pd.read_csv(CSV, header=None, names=['ID','CODE'])
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
1

The reason there are extra index columns add is because to_csv() writes an index per default, so you can either disable index when saving your CSV:

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

or you can specify an index column when reading:

df = pd.read_csv('file.csv', index_col=0)
Ando
  • 11
  • 1