How to create a pandas dataframe containing columns with special characters?

本秂侑毒 提交于 2019-12-25 14:12:54

问题


I try to create a Pandas data-frame from a list of dictionaries:

df = pandas.DataFrame(ls, columns = cols)

As a result I get the following error message:

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

I assume that the errors is caused by the fact that some values of the dictionary contains "special characters" (like ä or ö).

How can I make pandas to accept these characters?


回答1:


You need to ensure that your default encoding is set to unicode; it is ascii by default. Try

import sys
reload(sys)
sys.setdefaultencoding('utf-8')


来源:https://stackoverflow.com/questions/28983608/how-to-create-a-pandas-dataframe-containing-columns-with-special-characters

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