The following script for writing to a CSV file is going to run on a server which will automate the run.
d = {'col1': a, col2': b, col3': c,}
df = pandas.DataFrame(d, index = [0])
with open('foo.csv', 'a') as f:
df.to_csv(f, index = False)
The problem is, everytime I run it, the header gets copied to the CSV file. How can I modify this code to have the header copied to the CSV file only the first time its run, and never after that?
Any help will be appreciated :)
try this:
filename = '/path/to/file.csv'
df.to_csv(filename, index=False, mode='a', header=(not os.path.exists(filename)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With