1

I'am trying to export as csv a pandas dataframe with the function:

outcome.to_csv("/Users/john/out_1.csv") 

I get the following error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 191: ordinal not in range(128) 

how do I go to position 191 to check what's wrong?

Many thanks

4
  • possible duplicate of "UnicodeEncodeError: 'ascii' codec can't encode character" Commented Jul 30, 2015 at 11:24
  • @johnred it's position 191 in a string that is in your DataFrame. Perhaps you could put the whole stack trace, the DataFrame itself? Commented Jul 30, 2015 at 11:58
  • Could you try adding encoding='utf-8' to the function and see if that works? Eg outcome.to_csv("/Users/john/out_1.csv", encoding="utf-8") Commented Jul 30, 2015 at 12:47
  • 1
    if you open an ipython console and type: print u'\u2019' you will see what kind of character it is you are looking for. You should probably just start using python3 Commented Jul 30, 2015 at 13:48

1 Answer 1

1
 outcome.to_csv("/Users/john/out_1.csv",encoding="utf-8") 

On referring to the documentation of pandas.to_csv, we have the following details. It seems that for Python 2.7 the default is "ascii" which needs to be overridden to "utf-8"

encoding : string, optional

A string representing the encoding to use in the output file, defaults to ‘ascii’ on Python 2 and ‘utf-8’ on Python 3.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.