I am trying to create a csv file that contains metadata in the first few rows, followed by timeseries data, so it can be processed by another web application. My csv file should look like this:
Code: ABC1
Frequency: Monthly
Description: Blah Blah
-------------------
2/1/1947 11.7
3/1/1947 11.9
I can create a csv file of the metadata:
metadata=pd.Series([('code: ABC123'),('freqency: monthly'),('description: describecode'),('--------')])
metadata.to_csv("metadata.csv",index=False)
I can create a csv of the timeseries
a=pd.Series((11.7,11.9),index=pd.date_range('1947-01-02','1947-01-03'))
a.to_csv("data.csv")
But I can't work out how to merge them together into the format at the top.
By using pandas. DataFrame. to_csv() method you can write/save/export a pandas DataFrame to CSV File. By default to_csv() method export DataFrame to a CSV file with comma delimiter and row index as the first column.
Many "CSV" files embed metadata, for example in lines before the header row of the CSV document.
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.
>>> with open('test.csv', 'w') as fout:
... fout.write('meta data\n:')
... meta_data.to_csv(fout)
... ts.to_csv(fout)
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