如何创建包含带有特殊字符的列的 pandas 数据框? [英] How to create a pandas dataframe containing columns with special characters?
本文介绍了如何创建包含带有特殊字符的列的 pandas 数据框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试根据词典列表创建Pandas数据框:
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 ö
).
如何制作熊猫来接受这些字符?> p>
How can I make pandas to accept these characters?
推荐答案
您需要确保将默认编码设置为unicode;默认情况下为ascii。尝试
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')
这篇关于如何创建包含带有特殊字符的列的 pandas 数据框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文