如何写入csv的数据帧配置行前的头



我使用python脚本来管理来自采集系统,董事会ecc的不同日志文件。我正在构建一个大数据框架,它是其他dfs的合并,最后我把它放到csv文件中,在最后一行下面:

dF_COMP.to_csv(path1 + nomefile, sep=';', decimal= ',' , columns=columns)

我想在csv文件的第一行添加一行配置参数。

我可以用数据框架构建csv,然后再次打开它,然后添加行,但我认为这不是最好的主意,因为我希望构建更大的文件…有更好的主意吗?

我可以用数据框架构建csv,然后再次打开它,然后添加行,但我认为这不是最好的主意,因为我希望构建更大的文件…

相反:

with open(path1 + nomefile, 'w') as csvfile:
csvfile.write(f'# Config blah blahn')  # note the '#' as comment character
df.to_csv(csvfile, sep=';', decimal= ',', columns=columns)

使用pd.read_csv:

再次读取文件
pd.read_csv(path1 + nomefile, comment='#')  # to skip lines begin with '#'
# OR
pd.read_csv(path1 + nomefile, skiprows=1)  # to skip the first line