是否可以将熊猫系列附加到列表中
Posted
技术标签:
【中文标题】是否可以将熊猫系列附加到列表中【英文标题】:Is it possible to append a pandas series to a list 【发布时间】:2019-11-07 16:27:19 【问题描述】:我最近一直在做一个项目,预测梦幻超级联赛中最优秀的球队。在成功分析不同的特征和参数后,由于以下“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
我的团队()
运行代码后出现此错误: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
【问题讨论】:
【参考方案1】:Pandas 框架是可变的。因此,它们不能用作字典的键或集合的元素。
查看第一个堆栈跟踪中的第 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__
方法的第二个堆栈跟踪。其中,我想self
是injured
,key
是player
。
确实不能hash(player)
。
(它不能是第二个in
子句,因为start_position
是一个普通的Python 列表,而__contains__
堆栈跟踪来自pandas。)
我会从player
中提取姓名或其他ID,并在injured
中搜索;也许我会把injured
变成一组名字。
【讨论】:
感谢您的回复,但我确实研究了“ind”的类型,结果发现它是一个“int”,正如预期的那样测试代码为ind在 Top_points.index 中: print(type(ind)) row = Top_points.loc[ind] print(row.Position) print(type(row)) print('\n') 输出:以上是关于是否可以将熊猫系列附加到列表中的主要内容,如果未能解决你的问题,请参考以下文章