2

I am trying to add a title to a dataframe when I write it into csv. My problems is that I am not managing to add the title above the dataframe.

I followed the advice in this link (How to add blank rows before a data frame while using pandas.to_csv) and add the title before the to_csv part but it's giving me the same result.

My code is:

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')

And what I get is:

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 
semblable
  • 773
  • 1
  • 8
  • 26

1 Answers1

0

Change path to outfile in your table.to_csv() and see how it works. Namely,

table.to_csv(outfile, mode="a",header=["Bucket 20%","Bucket 40%","Bucket 60%","Bucket 80%","Bucket 100%",], line_terminator='\n')
Bill Huang
  • 4,491
  • 2
  • 13
  • 31