Python 2 DataFrame KeyError:u'\ x

时间:2017-04-26 04:25:19

标签: python python-2.7 python-unicode

代码:

from __future__ import unicode_literals

#...

print df.columns
print df[u'总资产']

输出:

Index([u'余额', u'参考市值', u'可用', u'总资产', u'盈亏', u'资产(资金股份)'], dtype='object')

Traceback (most recent call last): "user_code.py", line 36, in handle_data
    print df[u'总资产']   File "/home/server/y/envs/kuanke/lib/python2.7/site-packages/pandas/core/frame.py", line 1797, in __getitem__
    return self._getitem_column(key)   File "/home/server/y/envs/kuanke/lib/python2.7/site-packages/pandas/core/frame.py", line 1804, in _getitem_column
    return self._get_item_cache(key)   File "/home/server/y/envs/kuanke/lib/python2.7/site-packages/pandas/core/generic.py", line 1084, in _get_item_cache
    values = self._data.get(item)   File "/home/server/y/envs/kuanke/lib/python2.7/site-packages/pandas/core/internals.py", line 2851, in get
    loc = self.items.get_loc(item)   File "/home/server/y/envs/kuanke/lib/python2.7/site-packages/pandas/core/index.py", line 1572, in get_loc
    return self._engine.get_loc(_values_from_object(key))   File "pandas/index.pyx", line 134, in pandas.index.IndexEngine.get_loc (pandas/index.c:3824)   File "pandas/index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas/index.c:3704)   File "pandas/hashtable.pyx", line 686, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12280) File "pandas/hashtable.pyx", line 694, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12231)

KeyError: u'\xe6\x80\xbb\xe8\xb5\x84\xe4\xba\xa7'

另一个版本可以正常使用。

#...

print df.columns
print df[unicode('总资产')]

----更新---------------------------------------

import pandas as pd
df = pd.DataFrame(columns=[u'总资产'])
print df.columns
print df[u'总资产']
return

输出乱码

  

2016-04-05 09:30:00 - 信息 - 索引([u'æ»èμ产'],dtype ='object')   2016-04-05 09:30:00 - 信息 - 系列([],名称:æ»èμ产,dtype:   对象)

0 个答案:

没有答案