Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ensure pandas.DataFrame.to_csv is flush immediately

Is there a way to force pandas.DataFrame.to_csv flush the csv that it is writing?

In CSV file writing we can do the following (f1.flush)

with open("t.csv", 'w', encoding='utf-8') as f1:
    writer = csv.writer(f1, delimiter=',', quoting=csv.QUOTE_MINIMAL, lineterminator='\n')
    writer.writerow(header)  # header
    writer.writerow(row)
    f1.flush()
like image 316
william007 Avatar asked Jun 09 '18 03:06

william007


People also ask

Does pandas To_csv overwrite?

When you write pandas DataFrame to an existing CSV file, it overwrites the file with the new contents. To append a DataFrame to an existing CSV file, you need to specify the append write mode using mode='a' .

Does pandas read_csv close the file?

If you pass it an open file it will keep it open (reading from the current position), if you pass a string then read_csv will open and close the file.

How do you refresh a DataFrame in Python?

Pandas DataFrame update() Method The update() method updates a DataFrame with elements from another similar object (like another DataFrame). Note: this method does NOT return a new DataFrame. The updating is done to the original DataFrame.


1 Answers

when passing a file path to pandas.to_csv(), the function will open a file, write to it, and close the file.

df.to_csv('my_output_file.csv')
# the file will now be fully written and fully flushed

thus flushing definitely happens as part of the OS handling of closing the file.

there is no need or possibility to further flush, the complete contents of the dataframe will be in the file

like image 55
Aviad Rozenhek Avatar answered Sep 29 '22 06:09

Aviad Rozenhek