我正试图根据每个球员的统计数据,为MLB球队建立一个军刀优化的阵容顺序。我有一个统计数据框架,我从中提取数据并按重要性顺序添加到一个空列表中,然后重新排序列表以创建击球阵容
以下是击球顺序:
现在-球员在阵容中获得位置的顺序对优化也很重要。顺序是2,4,1,5,3,6,7,8,9。因此,阵容中的第二个位置是最重要的,因此OPS最高的个人应该首先被添加到列表中,这样他就不会根据他的统计数据被添加到阵容中的任何其他位置
所以,我有一个空列表,我开始根据最重要的玩家的统计数据来提取他们,然后从数据框中删除他们,这样我就可以再次提取,而无需选择他们两次
这是我的密码:
opt_lineup = []
opt_lineup.append((chosen_team[chosen_team['OPS']==chosen_team['OPS'].max()]['Player']))
chosen_team.drop(chosen_team['OPS'].idxmax(), inplace = True)
opt_lineup.append((chosen_team[chosen_team['OPS']==chosen_team['OPS'].max()]['Player']))
chosen_team.drop(chosen_team['OPS'].idxmax(), inplace = True)
opt_lineup.append((chosen_team[chosen_team['OBP']==chosen_team['OBP'].max()]['Player']))
chosen_team.drop(chosen_team['OBP'].idxmax(), inplace = True)
opt_lineup.append((chosen_team[chosen_team['SLG']==chosen_team['SLG'].max()]['Player']))
chosen_team.drop(chosen_team['SLG'].idxmax(), inplace = True)
opt_lineup.append((chosen_team[chosen_team['SLG']==chosen_team['SLG'].max()]['Player']))
chosen_team.drop(chosen_team['SLG'].idxmax(), inplace = True)
chosen_team.head(30)
我的问题是,当我将一名球员添加到空名单时,如果两名球员平局获得最高统计数据,那么他们都会被添加到名单中,而我只想获得第一名
本质上-我正在寻找一种类似于keep = 'first'
的解决方案,在使用.drop()
时使用,但在添加到列表时使用
谢谢
要确保只选择max条目中的一个条目(第一个条目),而不是选择与max值匹配的所有条目,可以修改代码以使用^{} ,类似于对
.drop
执行的操作你可以从^{} 的正式文件中看到:
请注意,我还将您的代码更改为使用^{} 格式,例如
而不是使用以下格式:
这是为了更好地避免COPYWARNING设置,还可以获得更好的执行时间/内存利用率
所以你的问题是多个玩家拥有最高的属性,而你只想添加第一个。max()方法返回列表中的最高值,list.index()方法查找列表中给定值的第一个索引。因此,让我们找到max ops值(例如),并在我们将使用的索引的列表中找到该值的第一个索引
下面是一个代码示例:
编辑:在将索引添加到击球顺序后,确保从每个列表中删除索引,否则您可能会得到重复的球员
相关问题 更多 >
编程相关推荐