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?
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.
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.
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.
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)
# 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)
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With