首页 文章

使用Unicode字符打印Pandas列

提问于
浏览
2

我有一个pandas数据帧,其中包含一个包含unicode编码名称的列 .

import pandas as pd

no_unicode = pd.Series(['Steve', 'Jason', 'Jake'])
yes_unicode = pd.Series(['tea', 'caf\xe9', 'beer'])

var_names = dict(no_unicode = no_unicode, yes_unicode = yes_unicode)

df = pd.DataFrame(var_names)

print(df)

我可以在ipython中打印数据帧,但是当我尝试在Sublimetext中打印数据帧时(使用py3)我收到错误 .

UnicodeEncodeError:'ascii'编解码器无法编码位置73中的字符'\ xe9':序数不在范围内(128)

我已经搜索了一个解决方案的高低(并且在过程中学到了很多关于unicode的内容)但是我无法弄清楚如何在Sublimetext中打印数据帧 .

任何帮助将不胜感激 .

1 回答

  • 3

    pandas.compat 中有一个非常有用的函数 u ,用于在unicode中创建值:

    In [26]:
    import pandas as pd
    from pandas.compat import u
    no_unicode = pd.Series(['Steve', 'Jason', 'Jake'])
    #yes_unicode = pd.Series(['tea', 'caf\xe9', 'beer'])
    yes_unicode = pd.Series(map(u,['tea', 'caf\xe9', 'beer']))
    var_names = dict(no_unicode = no_unicode, yes_unicode = yes_unicode)
    df = pd.DataFrame(var_names)
    print(df)
    
      no_unicode yes_unicode
    0      Steve         tea
    1      Jason        café
    2       Jake        beer
    
    [3 rows x 2 columns]
    

相关问题