首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何在pandas中修复这个错误,在那里我需要找到具有最高特征的元素?

我如何在pandas中修复这个错误,在那里我需要找到具有最高特征的元素?
EN

Stack Overflow用户
提问于 2020-07-21 11:21:42
回答 1查看 55关注 0票数 0

这是我的代码。我需要找到速度最快的元素(精灵宝可梦),HP,攻防。它们是有序的,最强的在上面。我不知道错误是从哪里来的。

代码语言:javascript
复制
     def strongestPokemon():
  strongPokemon_df = pd.DataFrame()
  each_type_df = []

  for type2 in pokemon_data['Type 2'].unique():
      sub_type = pokemon_data[pokemon_data['Type 2'] == type2]
      sub_type.sort_values(by=['HP','Attack','Defense','Speed'],ascending=[False,False,False,False],inplace=True)
      sub_type.reset_index(drop=True,inplace=True)
      top_pokemon = sub_type.loc[1]
      each_type_df.append(top_pokemon)

  strongPokemon_df = pd.DataFrame(each_type_df,columns=pk_data.columns)

  return strongPokemon_df

pk_strongest = strongestPokemon()
pk_strongest

这些是我得到的错误。很抱歉问了这么长的问题。我不确定为什么会发生这个错误。

代码语言:javascript
复制
   ValueError: 1 is not in range

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
6 frames
<ipython-input-87-fe71fd5a4e0f> in <module>()
     14   return strongPokemon_df
     15 
---> 16 pk_strongest = strongestPokemon()
     17 pk_strongest

<ipython-input-87-fe71fd5a4e0f> in strongestPokemon()
      7       sub_type.sort_values(by=['HP','Attack','Defense','Speed'],ascending=[False,False,False,False],inplace=True)
      8       sub_type.reset_index(drop=True,inplace=True)
----> 9       top_pokemon = sub_type.loc[1]
     10       each_type_df.append(top_pokemon)
     11 

/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py in __getitem__(self, key)
   1766 
   1767             maybe_callable = com.apply_if_callable(key, self.obj)
-> 1768             return self._getitem_axis(maybe_callable, axis=axis)
   1769 
   1770     def _is_scalar_access(self, key: Tuple):

/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py in _getitem_axis(self, key, axis)
   1963         # fall thru to straight lookup
   1964         self._validate_key(key, axis)
-> 1965         return self._get_label(key, axis=axis)
   1966 
   1967 

/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py in _get_label(self, label, axis)
    623             raise IndexingError("no slices here, handle elsewhere")
    624 
--> 625         return self.obj._xs(label, axis=axis)
    626 
    627     def _get_loc(self, key: int, axis: int):

/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py in xs(self, key, axis, level, drop_level)
   3535             loc, new_index = self.index.get_loc_level(key, drop_level=drop_level)
   3536         else:
-> 3537             loc = self.index.get_loc(key)
   3538 
   3539             if isinstance(loc, np.ndarray):

/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/range.py in get_loc(self, key, method, tolerance)
    350                 return self._range.index(new_key)
    351             except ValueError:
--> 352                 raise KeyError(key)
    353         return super().get_loc(key, method=method, tolerance=tolerance)
    354 

KeyError: 1
EN

回答 1

Stack Overflow用户

发布于 2020-07-21 11:30:33

尝试:

代码语言:javascript
复制
top_pokemon = sub_type.iloc[1]

iloc用于传递索引时,因为您想要第一行,所以使用:

代码语言:javascript
复制
top_pokemon = sub_type.iloc[0]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63006834

复制
相关文章

相似问题

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