使用utf-8编码在Pandas Dataframe中读取Csv文件,但在带有编码字符的Dataframe上打印操作结果失败

我使用utf-8编码在Pandas Dataframe中成功读取了一个Csv文件,但是在Dataframe上打印操作结果(在这种情况下,交叉表)会失败,因为UnicodeEncodeError'ascii'编解码器无法编码字符u'\ xb0' - 我应该在打印此中间数据帧之前再次编码/解码(下面的示例代码中为“p”)

import pandas as pd
    filehandle='/home/ekta/Desktop/test_data/df_30.csv'
    df0 = pd.read_csv(filehandle,skiprows=0, sep=',', encoding='utf-8',nrows=100)
    df=df0[['country', 'appname']]
    p=pd.crosstab(df['appname'], df['country'], rownames=['appname'], colnames=['country'])
    print p  #errr while printing this Dataframe

    """ Data frame read was success, both for df0 & df, but cross tab fails with and the error that I get *"UnicodeEncodeError: 'ascii' codec can't encode character u'\xb0' in position 13: ordinal not in range(128)"*. See "p" below  """

    #The dataframe, df looks like this 

       country                         appname
    0      SGP               Android Skout New
    2      SGP               Android Skout New
    3      SGP               Android Skout New
    7      SGP       Guess The Emoji - Android
    14     SGP             ScoreMobile Android
    15     IDN               Android Skout New
    16     IND  Truecaller - Caller ID & Block
    19     IDN                  Indonesia News
       ....More ... <Chopped>
    251    IDN      'Anonymous healthcare_and_fitness App cflw`2B2[h1s`lNzF@sPC1FtaCji:6kTF@']
    272    SGP    '(old) Weather\xc2\xb0'
# note the last two entries in this sample , there are more of these.

既然df的“读取”是成功的,这就是我想要理解的 - :

在将"Series"对象 (df['appname']) 传递给交叉表之前

  • Should I be doing the encode-> decode ?类似于 df['country'].encode('utf-8').decode('utf-8') [虽然这不是一个有效的语法]

  • 下面的片段转换为df - 这让我想到了问题, WHY ?

df0 ['country'] = df0 ['country'] . astype(unicode)
DF0 [ '国家']
0 S.
2 S.
3 S.
7 S.
14 S.
15我
16我
19我
21我
22我
25我
37我
41 S.
43我

My main question : 在我提供编码为"utf-8"之后,csv读取成功的帖子,为什么在我做交叉表时需要再次编码?我究竟做错了什么 ?

Note that the creation of "p" the dataframe from the pd.crosstab is NOT a problem, 只是"print p" . 我使用p作为中间对象,但想知道如何打印这个DataFrame - 我应该使用(1)技术吗?

回答(0)