python pandas read_excel 在describe()上返回UnicodeDecodeError

问题描述 投票:0回答:3

我喜欢 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)

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

解决这个问题的一种方法是剥离细胞,但一定有更好的东西。

for col in df.columns:
    df[col]=df[col].str.strip()

我使用的是anaconda2.2.0 win64,带有pandas 0.16

python excel pandas unicode
3个回答
3
投票

尝试一下这里建议的方法:

df=pd.read_excel('tmp.xlsx',encoding=sys.getfilesystemencoding())

1
投票

希望这对某人有帮助。

我遇到了这个错误:

UnicodeDecodeError: 'ascii' codec can't decode byte ....

读取 Excel 文件后

df = pd.read_excel...
并尝试将新列分配给数据框,如下所示
df['new_col'] = 'foo bar'

经过仔细检查,我发现了问题所在。由于缺少列标题,数据框中存在一些

'nan'
列。使用以下代码删除“nan”列后,其他一切都正常。

df = df.dropna(axis=1,how='all')

0
投票

df=pd.read_excel(r'粘贴路径') df.describe()

© www.soinside.com 2019 - 2024. All rights reserved.