3

I have a column in a Pandas dataframe which has mixed values, i.e. strings, floats and numbers. I would like to convert all values in this column to strings, but it does not let me as it says:

df['text'] = df['text'].astype(str)

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

I know that normally you can just convert a float by using

 str(0.05) --> '0.05'

But now when I cast the column as string, it still leaves the floats as floats.

Grr
  • 15,553
  • 7
  • 65
  • 85
Probs
  • 343
  • 2
  • 6
  • 20
  • Possible duplicate of [UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)](http://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20) – DYZ Apr 10 '17 at 15:39

1 Answers1

3

Since the column is unicode, you could try to encode it:

df['text'] = df['text'].apply(lambda x: x.encode('utf-8').strip())
fedterzi
  • 1,105
  • 7
  • 17