Printing Pandas Columns With Unicode Characters

大兔子大兔子 提交于 2021-02-10 12:29:48

问题


I have a pandas dataframe with a single column that contains a unicode encoded name.

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)

I can print the dataframe in ipython fine, but I get an error when I try to print the dataframe in Sublimetext (using py3).

UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 73: ordinal not in range(128)

I have searched high and low for a solution (and learned a lot about unicode in the process) but I cannot figure out how to print the dataframe while in Sublimetext.

Any help would be greatly appreciated.


回答1:


There is a very useful function u in pandas.compat, to make your values in 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]


来源:https://stackoverflow.com/questions/23014027/printing-pandas-columns-with-unicode-characters

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