I have pandas data frame where column type shows as object
but when I try to convert to string,
df['column'] = df['column'].astype('str')
UnicodeEncodeError
get thrown:
*** UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)
My next approach was to handle the encoding part:
df['column'] = filtered_df['column'].apply(lambda x: x.encode('utf-8').strip())
But that gives following error:
*** AttributeError: 'float' object has no attribute 'encode'
Whats the best approach to convert this column to string.
Sample of string in the column
Thank you :)
Thank You !!!
responsibilities/assigned job.
I had the same problem in python 2.7 when trying to run a script that was originally intended for python 3. In python 2.7, the default
str
functionality is to encode to ASCII, which will apparently not work with your data. This can be replicated in a simple example:Results in:
Instead, you can specify unicode:
Verify that the number has been converted to a string:
This outputs
u'123'
, so it has been converted to a unicode string. The special character ™ has been properly preserved as well.