Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a pandas DataFrame to CSV file

I have a dataframe in pandas which I would like to write to a CSV file.

I am doing this using:

df.to_csv('out.csv') 

And getting the following error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u03b1' in position 20: ordinal not in range(128) 
  • Is there any way to get around this easily (i.e. I have unicode characters in my data frame)?
  • And is there a way to write to a tab delimited file instead of a CSV using e.g. a 'to-tab' method (that I don't think exists)?
like image 976
user7289 Avatar asked Jun 04 '13 16:06

user7289


People also ask

How do I write a pandas DataFrame to a CSV file?

By using pandas. DataFrame. to_csv() method you can write/save/export a pandas DataFrame to CSV File. By default to_csv() method export DataFrame to a CSV file with comma delimiter and row index as the first column.

Can pandas write to CSV?

Pandas is a very powerful and popular framework for data analysis and manipulation. One of the most striking features of Pandas is its ability to read and write various types of files including CSV and Excel.

How do I export data from Jupyter notebook to CSV?

If you have data in pandas DataFrame then you can use . to_csv() function from pandas to export your data in CSV .


2 Answers

To delimit by a tab you can use the sep argument of to_csv:

df.to_csv(file_name, sep='\t') 

To use a specific encoding (e.g. 'utf-8') use the encoding argument:

df.to_csv(file_name, sep='\t', encoding='utf-8') 
like image 199
Andy Hayden Avatar answered Oct 12 '22 23:10

Andy Hayden


When you are storing a DataFrame object into a csv file using the to_csv method, you probably wont be needing to store the preceding indices of each row of the DataFrame object.

You can avoid that by passing a False boolean value to index parameter.

Somewhat like:

df.to_csv(file_name, encoding='utf-8', index=False) 

So if your DataFrame object is something like:

  Color  Number 0   red     22 1  blue     10 

The csv file will store:

Color,Number red,22 blue,10 

instead of (the case when the default value True was passed)

,Color,Number 0,red,22 1,blue,10 
like image 26
Sayan Sil Avatar answered Oct 13 '22 00:10

Sayan Sil