SanMu
SanMu

Reputation: 765

Pandas - appending to existing file using to_csv

I am trying to append a df to an existing file using the following code:

with open(file, 'a') as f:
    df.to_csv(f, header=False)
    f.close()

This almost gives me what I want, except that when I open the file, every record that was added is followed by a blank line, before the next valid record appears.

I have seen similar questions, but they were using built-in Python functions as opposed to pandas' to_csv.

Upvotes: 1

Views: 63

Answers (1)

Andrea
Andrea

Reputation: 3077

I would use directly the pandas API for appending to the csv:

df.to_csv(file, header=False, mode='a')

Upvotes: 1

Related Questions