Python 添加带有“的注释”;至“U csv”;使用熊猫

Python 添加带有“的注释”;至“U csv”;使用熊猫,python,pandas,Python,Pandas,我使用方法将数据帧保存到_csv,输出如下: 2015 04 08 0.0 14.9 2015 04 09 0.0 9.8 2015 04 10 0.3 23.0 但是我需要对这个输出进行一些修改,我需要添加一个注释和一个列,该列的值为常量,大小与其他列相同。我需要获得如下输出: #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

我使用
方法将数据帧保存到_csv
,输出如下:

2015 04 08 0.0 14.9
2015 04 09 0.0  9.8
2015 04 10 0.3 23.0
但是我需要对这个输出进行一些修改,我需要添加一个注释和一个列,该列的值为常量,大小与其他列相同。我需要获得如下输出:

#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

有人知道怎么做吗?

列名必须自动保存如果这确实是
pandas
dataframe,请键入以下内容进行检查:

print df.columns 

其中,
df
是您的
pd.DataFrame()

最简单的方法是先添加注释,然后附加数据框。下面给出了两种方法,其中有更多信息

读入测试数据 2.使用
重新打开文件以_csv(mode='a')

为什么不干脆
打开
文件并写入它?你能编辑你的帖子并添加你的数据框和脚本吗?它是数据框吗?pandas
to_csv
具有选项
标题,这是默认值,因此您的列名应该自动写入。Yas@MicahSmith说,首先写入csv,然后使用类似的方法将注释添加到文件顶部。早上好,这是我发现的唯一方法:
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)
# 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')