Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas DataFrame output end of csv

I wonder how to add new DataFrame data onto the end of an existing csv file? The to_csv doesn't mention such functionality.

like image 524
perigee Avatar asked Jun 16 '13 15:06

perigee


People also ask

What does to_csv do in pandas?

Pandas DataFrame to_csv() function converts DataFrame into CSV data. We can pass a file object to write the CSV data into a file. Otherwise, the CSV data is returned in the string format.

What does to_csv return?

to_csv() function write the given series object to a comma-separated values (csv) file/format. Parameter : path_or_buf : File path or object, if None is provided the result is returned as a string.

What is mode in to_csv?

Here's how to interpret the arguments in the to_csv() function: 'existing. csv': The name of the existing CSV file. mode='a': Use the 'append' mode as opposed to 'w' – the default 'write' mode. index=False: Do not include an index column when appending the new data.


2 Answers

You can append using to_csv by passing a file which is open in append mode:

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

Use header=None, so as not to append the column names.

In fact, pandas has a wrapper to do this in to_csv using the mode argument (see Joe's answer):

df.to_csv(f, mode='a', header=False) 
like image 119
Andy Hayden Avatar answered Oct 19 '22 09:10

Andy Hayden


You can also pass the file mode as an argument to the to_csv method

df.to_csv(file_name, header=False, mode = 'a') 
like image 40
Joe Hooper Avatar answered Oct 19 '22 07:10

Joe Hooper