python pandas read_excel在describe()上返回UnicodeDecodeError

hsi*_*ger 5 python unicode excel pandas

我喜欢大熊猫,但是我遇到了Unicode错误的实际问题。read_excel()返回可怕的Unicode错误:

import pandas as pd
df=pd.read_excel('tmp.xlsx',encoding='utf-8')
df.describe()

---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
...
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 259: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

我发现原来的Excel在许多单元格的末尾都有(不间断的空格),这可能是为了避免将长数字字符串转换为float。

解决此问题的一种方法是剥离细胞,但必须有更好的方法。

for col in df.columns:
    df[col]=df[col].str.strip()
Run Code Online (Sandbox Code Playgroud)

我正在使用anaconda2.2.0 win64和pandas 0.16

sky*_*ker 3

尝试这里建议的方法:

df=pd.read_excel('tmp.xlsx',encoding=sys.getfilesystemencoding())
Run Code Online (Sandbox Code Playgroud)