首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TypeError:无法理解的类型:'Int64Index‘

TypeError:无法理解的类型:'Int64Index‘
EN

Stack Overflow用户
提问于 2017-09-03 14:11:38
回答 1查看 9.1K关注 0票数 2

代码中引起问题的部分是

代码语言:javascript
运行
复制
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)

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

我得到以下错误

代码语言:javascript
运行
复制
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'

我猜这与变量类型不匹配有关,但我已经找过了,也找不到如何补救这个问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-03 14:17:02

我想你需要iloc

代码语言:javascript
运行
复制
summer17.iloc[result_rows,2] += 1

示例:

代码语言:javascript
运行
复制
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

它与以下内容相同:

代码语言:javascript
运行
复制
#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_rowslist还是1d array

代码语言:javascript
运行
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46024279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档