TypeError:不可用类型:' Int64Index'

时间:2017-09-03 14:11:39

标签: python-3.x pandas

我的代码中导致我出现问题的部分是

def Half_Increase(self):
    self.keg_count=summer17.iloc[self.result_rows,2].values[0]
    self.keg_count +=1
    summer17[self.result_rows,2] = self.keg_count
    print(keg_count)

因此,当按下按钮小部件时,将执行此功能。它应该从数据帧中的特定单元格中获取值,向其中添加1,然后将新值返回到数据帧。 (我不完全确定这是否是正确的方法。)

我收到以下错误

Exception in Tkinter callback
Traceback (most recent call last):

  File "C:\Python3.6\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "beerfest_program_v0.3.py", line 152, in Half_Increase
    summer17[self.result_rows,2] = self.keg_count
  File "C:\Python3.6\lib\site-packages\pandas\core\frame.py", line 2331, in __setitem__
    self._set_item(key, value)
  File "C:\Python3.6\lib\site-packages\pandas\core\frame.py", line 2397, in _set_item
    value = self._sanitize_column(key, value)
  File "C:\Python3.6\lib\site-packages\pandas\core\frame.py", line 2596, in _sanitize_column
    if broadcast and key in self.columns and value.ndim == 1:
  File "C:\Python3.6\lib\site-packages\pandas\core\indexes\base.py", line 1640, in __contains__
    hash(key)
  File "C:\Python3.6\lib\site-packages\pandas\core\indexes\base.py", line 1667, in __hash__
    raise TypeError("unhashable type: %r" % type(self).__name__)
TypeError: unhashable type: 'Int64Index'

我猜这与变量类型不匹配有关,但我看上去并且无法找到解决方法。

1 个答案:

答案 0 :(得分:2)

我认为你需要iloc

summer17.iloc[result_rows,2] += 1

样品:

summer17 = pd.DataFrame({'a':[1,2,3],
                         'b':[3,4,5],
                         'c':[5,9,7]})
#if reselt_rows is scalar
result_rows = 1

print(summer17)
   a  b  c
0  1  3  5
1  2  4  9
2  3  5  7

summer17.iloc[result_rows,2] += 1
print(summer17)
   a  b   c
0  1  3   5
1  2  4  10
2  3  5   7

与:

相同
#get value
keg_count=summer17.iloc[result_rows,2]
#increment
keg_count +=1
#set value
summer17.iloc[result_rows,2] = keg_count
print(summer17)
   a  b   c
0  1  3   5
1  2  4  10
2  3  5   7

但如果result_rowslist1d array

result_rows = [1,2]

#get all values per positions defined in result_rows 
#filter only first value by values[0]
keg_count=summer17.iloc[result_rows,2].values[0]
#increment 
keg_count +=1
#set all values of result_rows by incremented value 
summer17.iloc[result_rows,2] = keg_count
print(summer17)
   a  b   c
0  1  3   5
1  2  4  10
2  3  5  10