Encoding/decoding non-ASCII character when using Python Pandas

丶灬走出姿态 提交于 2019-12-25 07:39:33

问题


I have some data with non-ASCII characters. I attempted to take care of it using the following:

# coding=utf-8
import pandas as pd
from pandas import DataFrame, Series
import sys
import re
reload(sys)
sys.setdefaultencoding('latin1')

Though I have identified some records still giving me encoding/decoding problem. I have copied and pasted one of the problematic record (containing the name and location columns of the record) as below:

'Eugène Badeau'    'E, Québec (county/comté), Quebec, Canada'

Using the .decode('utf-8') adding to the exact text extraction it resolved the problem.

print 'Eugène Badeau   E, Québec (county/comté), Quebec, Canada'.decode('utf-8')
output: Eugène Badeau   E, Québec (county/comté), Quebec, Canada

So I try to use it to convert my pandas column:

df.name = df.name.str.encode('utf-8')

The location seems to be ok, but the name is still wrong:

print df.location[735]
print df.name[735]

output:
E, Québec (county/comté), Quebec, Canada
eugã¨ne badeau

回答1:


You could do apply combined with unidecode lib:

from unidecode import unidecode

df['name']=df['name'].apply( lambda x:  unidecode(unicode(x, encoding = "utf-8")))
df['location']=df['location'].apply( lambda x:  unidecode(unicode(x, encoding = "utf-8")))

;)



来源:https://stackoverflow.com/questions/32686804/encoding-decoding-non-ascii-character-when-using-python-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!