gpt4 book ai didi

python - 查找期间的 KeyError 以引用交叉表

转载 作者:太空宇宙 更新时间:2023-11-04 02:22:50 26 4
gpt4 key购买 nike

我有一个数据框:

dfAll = match ID    Team A Hero 1.1 Team A Hero 1.2   Team A Hero 1.3
123124140 (1, 2) (2, 3) (1, 3)
123123124 (4, 1) (3, 4) (1, 5)

等等..

我有一个交叉表:

dfEloCross =     1     2     3     4     5
1 NaN -1.0 +2.0 -8.0 +5.0

2 +1.0 NaN +2.5 +3.0 0

3 -2.0 -2.5 NaN +5.5 -3.5

4 +8.0 -3.0 -5.5 NaN +2.8

5 -5.0 0 +3.5 -2.8 NaN

我希望每场比赛都返回一个值,它是垂直的,而不是水平的。

df1 = matchups 1    matchups 2    matchups 3
+1.0 -2.5 -2.0
-8.0 -5.5 -5.0

我试过使用这段代码:

for uv in range(1, 6):
for xv in range(1,6):
dfAll['Matchup' + ' ' + str(uv) + '.' + str(xv)] = dfAll['Team A Hero' + ' ' + str(uv) + '.' + str(xv)].apply(lambda x: dfEloCross.lookup([x[0]],[x[1]])[0])

对于 Team A Hero 1.1 的第一列,它工作正常

但是为其他人返回关键错误KeyError: '找不到一个或多个列标签

我是初学者,所以我很乐意得到所有我能得到的帮助。提前致谢!

编辑

作为引用,这里是我的数据框的片段:

dfAll = {'Team A Hero 1.1': {0: '(22, 21)', 1: '(12, 3)', 2: '(6, 7)', 3: ' (13, 18)', 4: '(28, 7)', 5: '(9, 36)', 6: '(36, 7)', 7: '(9, 7)', 8: ' (4, 61)'}, 'Team A Hero 1.2': {0: '(22, 43)', 1: '(12, 15)', 2: '(6, 31)', 3: '( 13, 49)', 4: '(28, 13)', 5: '(9, 58)', 6: '(36, 13)', 7: '(9, 8)', 8: '( 4, 64)'}, 'Team A Hero 1.3': {0: '(22, 71)', 1: '(12, 30)', 2: '(6, 40)', 3: '(13 , 64)', 4: '(28, 56)', 5: '(9, 78)', 6: '(36, 30)', 7: '(9, 13)', 8: '(4 , 72)'}, 'Team A Hero 1.4': {0: '(22, 77)', 1: '(12, 65)', 2: '(6, 43)', 3: '(13, 65)', 4: '(28, 87)', 5: '(9, 95)', 6: '(36, 80)', 7: '(9, 15)', 8: '(4, 76)'}, 'Team A Hero 1.5': {0: '(22, 85)', 1: '(12, 103)', 2: '(6, 69)', 3: '(13, 107 )', 4: '(28, 106)', 5: '(9, 107)', 6: '(36, 98)', 7: '(9, 112)', 8: '(4, 84 )'}, 'Team A Hero 2.1': {0: '(28, 21)', 1: '(58, 3)', 2: '(20, 7)', 3: '(54, 18) ', 4: '(44, 7)', 5: '(28, 36)', 6: '(49, 7)', 7: '(52, 7)', 8: '(51, 61) '}}

最佳答案

您不需要使用嵌套循环。相反,您可以使用 pd.DataFrame.lookup Pandas 可用的方法。

在这种情况下,您可以迭代您的团队列,解压缩您的坐标并一次执行一个系列的查找:

from ast import literal_eval

df1 = pd.DataFrame({'matchID': [123124140, 123123124],
'TeamA_1.1': ['(1, 2)', '(4, 1)'],
'TeamA_1.2': ['(2, 3)', '(3, 4)']})

# convert mapping table columns to integer type
dfEloCross.columns = dfEloCross.columns.astype(int)

# calculate columns which need mapping
team_cols = df1.columns[df1.columns.str.startswith('Team')]

# cycle each column, strip column & row coordinates and use with lookup
for col in team_cols:
c, r = zip(*df1[col].apply(literal_eval))
df1[col] = dfEloCross.lookup(r, c)

print(df1)

TeamA_1.1 TeamA_1.2 matchID
0 1.0 -2.5 123124140
1 -8.0 -5.5 123123124

关于python - 查找期间的 KeyError 以引用交叉表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51175538/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com