15

I am trying to add a header to my CSV file.

I am importing data from a .csv file which has two columns of data, each containing float numbers. Example:

  11   22
  33   44
  55   66

Now I want to add a header for both columns like:

 ColA  ColB
  11    22
  33    44
  55    66

I have tried this:

with open('mycsvfile.csv', 'a') as f:
    writer = csv.writer(f)
    writer.writerow(('ColA', 'ColB'))

I used 'a' to append the data, but this added the values in the bottom row of the file instead of the first row. Is there any way I can fix it?

martineau
  • 119,623
  • 25
  • 170
  • 301
Sakil Chowdhury
  • 317
  • 1
  • 2
  • 8
  • You need to show a complete example that demonstrates the problem. – John Zwinck Jan 27 '15 at 02:42
  • Possible duplicate: http://stackoverflow.com/questions/4454298/prepend-a-line-to-an-existing-file-in-python – abought Jan 27 '15 at 02:46
  • i already saw that prob, but i think that differs from .csv file perspective. – Sakil Chowdhury Jan 27 '15 at 02:51
  • 3
    There is no way to write to only the beginning of a file. The workaround is to write out the entire file, with the new info at the beginning, – Paul Jan 27 '15 at 02:58
  • Can you just edit the file manually? Use a text editor or write the header to another file and don't use python, but simply use `cat` to concatenate the two. Do you have thousands of CSV files to which you need to append headers? – Sergey Orshanskiy Jan 27 '15 at 04:33

6 Answers6

32

One way is to read all the data in, then overwrite the file with the header and write the data out again. This might not be practical with a large CSV file:

#!python3
import csv
with open('file.csv',newline='') as f:
    r = csv.reader(f)
    data = [line for line in r]
with open('file.csv','w',newline='') as f:
    w = csv.writer(f)
    w.writerow(['ColA','ColB'])
    w.writerows(data)
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
10

i think you should use pandas to read the csv file, insert the column headers/labels, and emit out the new csv file. assuming your csv file is comma-delimited. something like this should work:

   from pandas import read_csv

   df = read_csv('test.csv')
   df.columns = ['a', 'b']
   df.to_csv('test_2.csv')
adrianX
  • 619
  • 7
  • 21
  • 2
    Thank you for this comment. When I do this, my first row is replaced with the header. How can I append the header while keeping all my rows? Thank you – user710 Sep 08 '20 at 21:48
8

I know the question was asked a long time back. But for others stumbling across this question, here's an alternative to Python.

If you have access to sed (you do if you are working on Linux or Mac; you can also download Ubuntu Bash on Windows 10 and sed will come with it), you can use this one-liner:

sed -i 1i"ColA,ColB" mycsvfile.csv

The -i will ensure that sed will edit in-place, which means sed will overwrite the file with the header at the top. This is risky.

If you want to create a new file instead, do this

sed 1i"ColA,ColB" mycsvfile.csv > newcsvfile.csv
Mishal Ahmed
  • 191
  • 2
  • 11
2

In this case, You don't need the CSV module. You need the fileinput module as it allows in-place editing:

import fileinput

for line in fileinput.input(files=['mycsvfile.csv'], inplace=True):
    if fileinput.isfirstline():
        print 'ColA,ColB'
    print line,

In the above code, the print statement will print to the file because of the inplace=True parameter.

Hai Vu
  • 37,849
  • 11
  • 66
  • 93
1

For the issue where the first row of the CSV file gets replaced by the header, we need to add an option.

import pandas as pd  
df = pd.read_csv('file.csv', **header=None**)  
df.to_csv('file.csv', header = ['col1', 'col2']) 
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
-1

You can set reader.fieldnames in your code as list like in your case

 with open('mycsvfile.csv', 'a') as fd:
        reader = csv.DictReader(fd)
        reader.fieldnames = ["ColA" , "ColB"]
        for row in fd