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 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 dont think exists)?
If you don't want the index.
To delimit by a tab you can use the
sep
argument ofto_csv
:To use a specific encoding (e.g. 'utf-8') use the
encoding
argument:Sometimes you face these problems if you specify UTF-8 encoding also. I recommend you to specify encoding while reading file and same encoding while writing to file. This might solve your problem.
It will work definitely.
Change
df
to the name of your dataframe name and run.Use anaconda idle.
I'd like to add something to what Andy Hayden already mentioned in his answer.
When you are storing a
DataFrame
object into a csv file using theto_csv
method, you probably wont be needing to store the preceding indices of each row of theDataFrame
object.You can avoid that by passing a
False
boolean value toindex
parameter.Somewhat like:
So if your DataFrame object is something like:
The csv file will store:
instead of (the case when the default value
True
was passed)Found it worth sharing, Cheers! :-)
Something else you can try if you are having issues encoding to 'utf-8' and want to go cell by cell you could try the following.
Python 2
(Where "df" is your DataFrame object.)
Then try:
You can check the encoding of the columns by:
Warning: errors='ignore' will just omit the character e.g.
Python 3