Pandas 熊猫和列表中的瑞典字符

Pandas 熊猫和列表中的瑞典字符,pandas,special-characters,Pandas,Special Characters,我使用的是Python2.7和Pandas,但瑞典字符有问题。瑞典字符在熊猫中工作,但当我从数据框创建列表时,出现了一些错误: import pandas as pd d = {'name': ['Åberg', 'Östlund', 'Älberg', 'Ericsson'], 'age': [22,38,26,35] } df = pd.DataFrame(d) print(df) age name 0 22 Åbe

我使用的是Python2.7和Pandas,但瑞典字符有问题。瑞典字符在熊猫中工作,但当我从数据框创建列表时,出现了一些错误:

import pandas as pd 

d = {'name': ['Åberg', 'Östlund', 'Älberg', 'Ericsson'],
     'age': [22,38,26,35] 
    }

    df = pd.DataFrame(d)

    print(df)


   age      name
0   22     Åberg
1   38   Östlund
2   26    Älberg
3   35  Ericsson

df['name'].tolist()

['\xc3\x85berg', '\xc3\x96stlund', '\xc3\x84lberg', 'Ericsson']

知道如何将瑞典语字符保留在列表中吗?

它们保留在列表中,但是python 2呈现unicode字符串。您可以通过打印每个值看到这一点:

In [11]: for name in df.name.tolist(): print(name)
Åberg
Östlund
Älberg
Ericsson
可以使用“连接”渲染连接列表:

In [12]: print(", ".join(df.name.tolist()))
Åberg, Östlund, Älberg, Ericsson
您可能希望明确地确保它们是unicode,并使用:

In [13]: [n.decode("utf-8") for n in df.name.tolist()]
Out[13]: [u'\xc5berg', u'\xd6stlund', u'\xc4lberg', u'Ericsson']
但正如您所看到的,它们仍然会呈现出笨拙的效果

(最好的解决方案是更新到python 3!;)


注意:Python 3按照列表中的预期呈现这些Unicode:

In [31]: df.name.tolist()
Out[31]: ['Åberg', 'Östlund', 'Älberg', 'Ericsson']