我正在尝试将一个dataframe写入csv文件时添加标题。我的问题是我没有成功在dataframe上方添加标题。
我遵循了这个链接中的建议(如何在使用pandas.to_csv时在dataframe之前添加空白行),并在to_csv部分之前添加了标题,但结果仍然相同。
我的代码是:
我遵循了这个链接中的建议(如何在使用pandas.to_csv时在dataframe之前添加空白行),并在to_csv部分之前添加了标题,但结果仍然相同。
我的代码是:
tables = [table_bin_fill_temp_regime, table_velocity_temp_regime, table_count_temp_regime,
table_bin_fill_chill_preference, table_velocity_chill_preference, table_count_chill_preference]
table_names = ["Average Bin_fill per temperature regime per Pareto bucket",
"Average Velocity per temperature regime per Pareto bucket",
"Average number or skus per temperature regime per Pareto bucket"]
for table in tables:
for table_name in table_names:
with open(path, 'a') as outfile:
outfile.write(table_name)
outfile.write('\n')
table.to_csv(path, mode="a",header=["Bucket 20%","Bucket 40%","Bucket 60%","Bucket 80%","Bucket 100%",], line_terminator='\n')
outfile.write('\n')
我得到的是:
temp_regime Bucket 20% Bucket 40% Bucket 60% Bucket 80% Bucket 100%
AMBIENT 0.012320481 0.004642788 0.002300375 0.001441901 0.000746188
CHILLED 0.056461032 0.01953134 0.008969644 0.004854571 0.001559037
Average Bin_fill per temperature regime per Pareto bucket
I am aiming for:
Average Bin_fill per temperature regime per Pareto bucket
temp_regime Bucket 20% Bucket 40% Bucket 60% Bucket 80% Bucket 100%
AMBIENT 0.012320481 0.004642788 0.002300375 0.001441901 0.000746188
CHILLED 0.056461032 0.01953134 0.008969644 0.004854571 0.001559037