Skip to content Skip to sidebar Skip to footer

How To Add Blank Rows Before A Data Frame While Using Pandas.to_csv

I want to write a dataframe to a file dataToGO = {'Midpoint': xdata1, '': '', 'Avg Diam': ydata1, '' : ''} colums = ['Midpoint', '', 'Avg Diam', '']

Solution 1:

Use append mode:

withopen(‘processed’+filname+’.csv’, ‘a’) as f:
    f.write(‘\n’*10)
    ToFile.to_csv(f, header=False)

Solution 2:

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

Solution 3:

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.

Post a Comment for "How To Add Blank Rows Before A Data Frame While Using Pandas.to_csv"