本文介绍了在Python/Pandas中创建带有元数据标头和时间序列的csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个csv文件,该文件在前几行中包含元数据,后跟时间序列数据,因此可以由另一个Web应用程序对其进行处理.我的csv文件应如下所示:
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
我可以创建元数据的csv文件:
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)
我可以创建时间序列的csv
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.
推荐答案
>>> with open('test.csv', 'w') as fout:
... fout.write('meta data\n:')
... meta_data.to_csv(fout)
... ts.to_csv(fout)
这篇关于在Python/Pandas中创建带有元数据标头和时间序列的csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!