收到`KeyError:你没有名为XYZ'的项目错误

时间:2013-12-31 08:09:20

标签: python csv pandas

这是我收到的错误 运行后我有这个问题:

Traceback (most recent call last):
  File "t1.py", line 255, in <module>
    pivot_rating = ratings.pivot(index='User-ID', columns='ISBN', values='Book-Rating')
  File "/Library/Python/2.7/site-packages/pandas/core/frame.py", line 4182, in pivot
    return pivot(self, index=index, columns=columns, values=values)
  File "/Library/Python/2.7/site-packages/pandas/core/reshape.py", line 343, in pivot
    indexed = Series(self[values].values,
  File "/Library/Python/2.7/site-packages/pandas/core/frame.py", line 2003, in __getitem__
    return self._get_item_cache(key)
  File "/Library/Python/2.7/site-packages/pandas/core/generic.py", line 667, in _get_item_cache
    values = self._data.get(item)
  File "/Library/Python/2.7/site-packages/pandas/core/internals.py", line 1655, in get
    _, block = self._find_block(item)
  File "/Library/Python/2.7/site-packages/pandas/core/internals.py", line 1935, in _find_block
    self._check_have(item)
  File "/Library/Python/2.7/site-packages/pandas/core/internals.py", line 1942, in _check_have
    raise KeyError('no item named %s' % com.pprint_thing(item))
KeyError: u'no item named Book-Rating'
Line 255 is pivot_rating = ratings.pivot(index='User-ID', columns='ISBN', values='Book-Rating')

运行以下代码后:

ratings = pd.read_csv('/Users/mona/Downloads/BX-Dump/BX-Book-Ratings.csv', sep=";", quotechar="\"", escapechar="\\")

print ratings
# <class 'pandas.core.frame.DataFrame'> 
# Int64Index: 1149779 entries, 0 to 1149778 
# Data columns (total 3 columns): 
# 276725 1149779 non-null values 
# 034545104X 1149779 non-null values 
# 0 1149779 non-null values

pivot_rating = ratings.pivot(index='User-ID', columns='ISBN', values='Book-Rating')

但是文件BX-Book-Ratings.csv有三列,第三列是评分,每行都是这样的"276725";"034545104X";"0"。知道什么是错的吗?

1 个答案:

答案 0 :(得分:5)

您的ratings DataFrame没有名为'Book-Rating'的列:

>>> ratings = pd.DataFrame([[276725,"034545104X",0]], 
...                columns=['User-ID', 'ISBN', 'Book-Rating'])
>>> ratings.pivot(index='User-ID', columns='ISBN', values='Book-Rating')
ISBN     034545104X
User-ID
276725            0

与:比较:

>>> ratings.columns = ['User-ID', 'ISBN', 'BookRating']
>>> ratings.pivot(index='User-ID', columns='ISBN', values='Book-Rating')
Traceback (most recent call last):
  ...
KeyError: u'no item named Book-Rating'

<强>更新

您的案例中的解决方案是在代码中指定使用names paramether加载数据的列。尝试替换

pd.read_csv('/Users/mona/Downloads/BX-Dump/BX-Book-Ratings.csv', 
     sep=";", quotechar="\"", escapechar="\\")

pd.read_csv('/Users/mona/Downloads/BX-Dump/BX-Book-Ratings.csv', 
     header=None, names=['User-ID', 'ISBN', 'Book-Rating'],
     sep=";", quotechar="\"", escapechar="\\")