7

I'm saving a dataframe with the method to_csv, and the output is like that:

2015 04 08 0.0 14.9
2015 04 09 0.0  9.8
2015 04 10 0.3 23.0

but I need to make some modifications to this output, I need to add a comment and a column with a constant value and the same size that the others columns. I need to obtain an output like this one:

#Data from the ...
#yyyy mm dd pcp temp er
2015 04 08 0.0 14.9 0
2015 04 09 0.0  9.8 0
2015 04 10 0.3 23.0 0

Does anyone know how to do that?

Falko
  • 17,076
  • 13
  • 60
  • 105
Vanessa
  • 139
  • 1
  • 3
  • 9
  • 1
    Why not just `open` the file and write to it? – Micah Smith Jul 06 '15 at 19:31
  • Can you edit your post and add your dataframe and your script ? – Mir Ilias Jul 06 '15 at 19:31
  • 1
    Is it pandas data frame? pandas `to_csv` has option `header`, which is default True, so your columns names should be written automatically – lanenok Jul 06 '15 at 19:39
  • As @MicahSmith says, write the CSV first, then use something like [this](http://stackoverflow.com/a/5917395/2071807) to add the comment to the top of the file. – LondonRob Jul 06 '15 at 19:43
  • Good morning, this is the only way I found:
      line= "#GEO \n#lat    long    level   date    time    value \n#DATA\n"                                                                                         with open(Filename, 'w') as f:
        f.write(line)
    df1.to_csv(auxiliar, index = False, sep='\t', float_format='%.6f', header = False)
        with open(nombreFichero, 'a') as f:
             with open(auxiliar, 'rb') as g:
                  shutil.copyfileobj(g, f)  
    – Vanessa Jul 13 '15 at 07:58

1 Answers1

8

The easiest approach is to add the comment first and then append the data frame. Two approaches to this are given below, and there is more info in Write comments in CSV file with pandas.

Read in test data

import pandas as pd
# Read in the iris data frame from the seaborn GitHub location
iris = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')
# Create a bigger data frame
while iris.shape[0] < 100000:
    iris = iris.append(iris)
# `iris.shape` is now (153600, 5)

1. Append with the same file handler

# Open a file in append mode to add the comment
# Then pass the file handle to pandas
with open('test1.csv', 'a') as f:
    f.write('# This is my comment\n')
    iris.to_csv(f)

2. Reopen the file with to_csv(mode='a')

# Open a file in write mode to add the comment
# Then close the file and reopen it with pandas in append mode
with open('test2.csv', 'w') as f:
    f.write('# This is my comment\n')
iris.to_csv('test2.csv', mode='a')
joelostblom
  • 43,590
  • 17
  • 150
  • 159