UnicodeDecodeError in pandas frame generated from JSON file

I have a piece of code running on an iPython notebook that loads a JSON file and then parses the content into Pandas DF. However, if I try to check the DF, I get an encoding error.

output = r.json()
columns_map = {'/people/person/date_of_birth': 'birth_date',
              '/people/person/place_of_birth': 'birth_place',
              '/people/person/gender': 'gender'}
dF = pd.DataFrame(output['result'])
dF.rename(columns=columns_map, inplace=True)
dF.to_csv('file.csv',encoding='utf-8')

      

I can create CSV from DF without any problem, but if I type

dF

      

To check dF inside iPython notebook I get this:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1894: ordinal not in range(128)

      

Does anyone help?

+3


source to share


1 answer


After some research, I found this to be a problem with Python version <3.0. For some strange reason, a quick solution is to import sys and relaod sys. This worked for me:



import sys    
reload(sys)  
sys.setdefaultencoding('utf8')

      

+7


source







All Articles