我喜欢 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
尝试一下这里建议的方法:
df=pd.read_excel('tmp.xlsx',encoding=sys.getfilesystemencoding())
希望这对某人有帮助。
我遇到了这个错误:
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')
df=pd.read_excel(r'粘贴路径') df.describe()