我在转动表时遇到问题。我有一个名为Rate的pandas数据框中的数据,其中我有该用户的用户ID,book-id和该书的评级。
像这样:
User Book Rating
user1 book1 2.5
user3 book7 3.0
user1 book3 2.0
... ... ...
我希望将所有唯一的User作为行,所有唯一的书籍作为列以及它们相应的评级(3或2或4.5等)作为值来转动表格。我正在使用这一行
y = Rate.pivot(index=Rate.columns[0], columns=Rate.columns[1], values=Rate.columns[2])
但我有这个错误:
"---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-29-7c13513a221b> in <module>()
1 #Rate.columns = ['User','Book','Ratings']
----> 2 y = Rate.pivot(index=Rate.columns[0], columns=Rate.columns[1], values=Rate.columns[2])
C:\Users\conne\Anaconda3\lib\site-packages\pandas\core\frame.py in pivot(self, index, columns, values)
3844 """
3845 from pandas.core.reshape import pivot
-> 3846 return pivot(self, index=index, columns=columns, values=values)
3847
3848 def stack(self, level=-1, dropna=True):
C:\Users\conne\Anaconda3\lib\site-packages\pandas\core\reshape.py in pivot(self, index, columns, values)
330 indexed = Series(self[values].values,
331 index=MultiIndex.from_arrays([index, self[columns]]))
--> 332 return indexed.unstack(columns)
333
334
C:\Users\conne\Anaconda3\lib\site-packages\pandas\core\series.py in unstack(self, level, fill_value)
2041 """
2042 from pandas.core.reshape import unstack
-> 2043 return unstack(self, level, fill_value)
2044
2045 # ----------------------------------------------------------------------
C:\Users\conne\Anaconda3\lib\site-packages\pandas\core\reshape.py in unstack(obj, level, fill_value)
405 else:
406 unstacker = _Unstacker(obj.values, obj.index, level=level,
--> 407 fill_value=fill_value)
408 return unstacker.get_result()
409
C:\Users\conne\Anaconda3\lib\site-packages\pandas\core\reshape.py in __init__(self, values, index, level, value_columns, fill_value)
99
100 self._make_sorted_values_labels()
--> 101 self._make_selectors()
102
103 def _make_sorted_values_labels(self):
C:\Users\conne\Anaconda3\lib\site-packages\pandas\core\reshape.py in _make_selectors(self)
134 selector = self.sorted_labels[-1] + stride * comp_index + self.lift
135 mask = np.zeros(np.prod(self.full_shape), dtype=bool)
--> 136 mask.put(selector, True)
137
138 if mask.sum() < len(self.index):
IndexError: index 1495360453 is out of bounds for axis 0 with size 1495018980"
有人可以解释一下为什么会收到错误以及如何删除错误?请注意,唯一身份用户的数量不等于唯一身份证的数量!
答案 0 :(得分:0)
这显然是超出规模的问题。假设你有一个大小为3的数组,并尝试在内部写第4个。