我想在我用pandas创建的CSV文件中写一些注释。我在DataFrame.to_csv (尽管read_csv可以跳过注释)中没有找到任何选项,在标准的csv模块中也没有。我可以打开文件,编写注释(以#开头的行),然后将其传递给to_csv。有没有人有更好的选择?
发布于 2015-03-24 21:28:27
df.to_csv接受文件对象。因此,您可以在a模式下打开文件,编写注释并将其传递给dataframe to_csv函数。
例如:
In [36]: df = pd.DataFrame({'a':[1,2,3], 'b':[1,2,3]})
In [37]: f = open('foo', 'a')
In [38]: f.write('# My awesome comment\n')
In [39]: f.write('# Here is another one\n')
In [40]: df.to_csv(f)
In [41]: f.close()
In [42]: more foo
# My awesome comment
# Here is another one
,a,b
0,1,1
1,2,2
2,3,3https://stackoverflow.com/questions/29233496
复制相似问题