问题
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