df.to_csv接受文件对象。因此,您可以在
a模式下打开文件,编写注释并将其传递到数据框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 commentn')In [39]: f.write('# Here is another onen')In [40]: df.to_csv(f)In [41]: f.close()In [42]: more foo# My awesome comment# Here is another one,a,b0,1,11,2,22,3,3


