首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >是否可以将熊猫系列添加到列表中

是否可以将熊猫系列添加到列表中
EN

Stack Overflow用户
提问于 2019-06-26 03:19:14
回答 1查看 486关注 0票数 1

我最近一直在做一个项目,它预测了梦幻超级联赛中最好的球队。在成功地分析了不同的特征和参数之后,我遇到了以下问题:"TypeError:'Series‘对象是可变的,因此它们不能被散列“。

我已经完成了代码的第一部分,但收到一个错误。我在网上找了找,但找不到解决办法。其中一个解决方案说,您不能将序列追加到列表中。这是真的吗?对于同样的问题,可能的解决方案是什么。我已经走得太远了,我真的想把这件事做好。

def my_team (budget = 100, star_player_limit = 3, gk = 2, df = 5, mid = 5, fwd = 3 ): # Pass constraints to function
    team = [ ]                          # List of team to be returned
    star_position = [ ]                 # list containing position of starplayer
    star_player_limit = star_player_limit
    budget = budget
    injured = dataset2.loc[(dataset2.loc[:,"Status"] == 'injured'),:] # Keeping a check of injury status
    positions = {"GKP":gk,"DEF":df,"MID":mid,"FWD":fwd}       # Dict accounting for no. of postions left to fill
    for ind in Top_points.index:       # Looping through the dataframe of players
        player = Top_points.loc[ind]   # Row of Dataframe one at a time
        star_position.append(player.Position)    # Checking position of star player
        if len(team) < star_player_limit and player not in injured and budget > player.Cost and positions[player.Position] > 0 and player.Position not in star_position:
            team.append(player)
            budget -= player.Cost
            positions[player.Position] -= 1

    return team

my_team()

在运行代码之后,我得到了这个错误:TypeError: 'Series' objects are mutable, thus they cannot be hashed

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-150-a7d781e901c6> in <module>()
----> 1 my_team()

<ipython-input-149-ec17dbd9b9ba> in my_team(budget, star_player_limit, gk, df, mid, fwd)
      9         player = Top_points.loc[ind]
     10         star_position.append(player.Position)
---> 11         if len(team) < star_player_limit and player not in injured and budget > player.Cost and positions[player.Position] > 0 and player.Position not in star_position:
     12             team.append(player)
     13             budget -= player.Cost

~\Anaconda3\lib\site-packages\pandas\core\generic.py in __contains__(self, key)
   1517     def __contains__(self, key):
   1518         """True if the key is in the info axis"""
-> 1519         return key in self._info_axis
   1520 
   1521     @property

~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in __contains__(self, key)
   2018     @Appender(_index_shared_docs['__contains__'] % _index_doc_kwargs)
   2019     def __contains__(self, key):
-> 2020         hash(key)
   2021         try:
   2022             return key in self._engine

~\Anaconda3\lib\site-packages\pandas\core\generic.py in __hash__(self)
   1487     def __hash__(self):
   1488         raise TypeError('{0!r} objects are mutable, thus they cannot be'
-> 1489                         ' hashed'.format(self.__class__.__name__))
   1490 
   1491     def __iter__(self):

TypeError: 'Series' objects are mutable, thus they cannot be hashed
EN

回答 1

Stack Overflow用户

发布于 2019-06-26 03:28:12

熊猫框架是可变的。因此,它们不能用作dict的键,也不能用作set的元素。

看看第一个堆栈跟踪中的第11行。我已经将其重新格式化,使其具有可读性。

if (len(team) < star_player_limit and
    player not in injured and
    budget > player.Cost and
    positions[player.Position] > 0 and
    player.Position not in star_position):

我们这里有一个player not in injured子句。前者被定义为

player = Top_points.loc[ind] 

我认为它的类型是Series

现在我们有了处理in操作符的__contains__方法的第二个堆栈跟踪。在其中,我假设selfinjuredkeyplayer。事实上,它不能hash(player)

(它不能是第二个in子句,因为start_position是一个普通的Python列表,而__contains__堆栈跟踪来自pandas。)

我会从player中提取名称或其他ID,并在injured中搜索它;也许我会在此过程中将injured转换为一组名称。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56760712

复制
相关文章

相似问题

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