Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding a comment with "to_csv" using pandas [duplicate]

Tags:

python

pandas

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?

like image 989
Vanessa Avatar asked Jul 06 '15 19:07

Vanessa


People also ask

Does pandas To_csv overwrite?

Does pandas To_csv overwrite? If the file already exists, it will be overwritten. If no path is given, then the Frame will be serialized into a string, and that string will be returned.

What does To_csv do in pandas?

Pandas DataFrame to_csv() function converts DataFrame into CSV data. We can pass a file object to write the CSV data into a file. Otherwise, the CSV data is returned in the string format.


1 Answers

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')
like image 89
joelostblom Avatar answered Sep 16 '22 17:09

joelostblom