0

I want to write a dataframe to a file

            dataToGO = {'Midpoint': xdata1, '': "", 'Avg Diam': ydata1, '' : ""}
            colums = ['Midpoint', '', 'Avg Diam', '']
            ToFile = pad.DataFrame(data=dataToGO, columns=colums)

            ToFile.to_csv("processed"+filname+".csv", index=False)

However, I want to add 10 blank lines in the file before the contents of the data frame. How do I do that?

3 Answers3

0

you can try something like this, open the file first, write whatever you want, then write the dataframe

a = open("test.csv","w")
a.write("\n")
df.to_csv(a)
a.close()
saucoide
  • 11
  • 2
0

I'm not sure how to do this before the data frame is saved down, but you could save it to a file and then reopen the file in python, "prepend" the 10 blank lines and then resave. It depends how big your data is and how many data frames you want to export to csv as to whether this would be an efficient method.

Alternatively, If you are just doing it for one csv file it could be easier to just open the csv file in Excel / Notepad++ etc and just manually add the lines.

0

Use append mode:

with open(‘processed’+filname+’.csv’, ‘a’) as f:
    f.write(‘\n’*10)
    ToFile.to_csv(f, header=False)
Superspork
  • 393
  • 2
  • 9