![](/img/trans.png)
[英]How to pass dataframes to a function using applymap() in pandas?
[英]Pandas - ApplyMap using corresponding cell values from two different DataFrames
我有两个熊猫数据框,想应用一个函数“Stat / Percentage Played”。 第一个数据框 - “Stat”如下所示:
- ----- ------- ------- ------- -------
0 Name Round 1 Round 2 Round 3 Round 4
1 Tom 10 22 18 21
2 Angus 13 16 21 19
3 Jack 18 19 16 17
4 Harry 21 19 15 19
5 Ben 19 24 22 27
- ----- ------- ------- ------- -------
第二个数据框显示了球员在球场上花费的时间(百分比),如下所示:
- ----- ------- ------- ------- -------
0 Name Round 1 Round 2 Round 3 Round 4
1 Tom 65 80 81 80
2 Angus 72 76 85 79
3 Jack 79 81 68 75
4 Harry 79 78 62 77
5 Ben 81 84 76 85
- ----- ------- ------- ------- -------
有了这些,我想将两个数据帧中的每个单元格应用到每个单元格,然后从该轮中获取相应的玩家统计数据,然后除以该玩家在该轮中的上场时间。 应用于每个单元格的函数如下所示:
- ----- ------- ------- ------- -------
0 Name Round 1 Round 2 Round 3 Round 4
1 Tom 10/65 22/80 18/81 21/80
2 Angus 13/72 16/76 21/85 19/79
3 Jack 18/79 19/81 16/68 17/75
4 Harry 21/79 19/78 15/62 19/77
5 Ben 19/81 24/84 22/76 27/85
- ----- ------- ------- ------- -------
此外,我的最终数据框中的最终值应如下所示:
- ----- ------------------- ------------------- ------------------- -------------------
0 Name Round 1 Round 2 Round 3 Round 4
1 Tom 0.15384615384615385 0.275 0.2222222222222222 0.2625
2 Angus 0.18055555555555555 0.21052631578947367 0.24705882352941178 0.24050632911392406
3 Jack 0.22784810126582278 0.2345679012345679 0.23529411764705882 0.22666666666666666
4 Harry 0.26582278481012656 0.24358974358974358 0.24193548387096775 0.24675324675324675
5 Ben 0.2345679012345679 0.2857142857142857 0.2894736842105263 0.3176470588235294
- ----- ------------------- ------------------- ------------------- -------------------
我能找出方法的唯一方法是转换为列表并使用循环来完成此功能,例如:
datlist = df.values.tolist()
headerList = datlist[:1] # Sava Header row to merge after calulcations
statList = datlist[1:] # Remove Header row for calculations
perlist = dfPerc.values.tolist()[1:] # Remove Header row for calculations
AdjList = []
for z in range(len(statList)):
PlayerList = []
for i in range(len(statList[z])):
if isinstance(statList[z][i], int) == True:
adjStat = statList[z][i] / perlist[z][i]
else:
adjStat = statList[z][i]
PlayerList.append(adjStat)
AdjList.append(PlayerList)
AdjList.insert(0, headerList[0])
finaldf = pd.DataFrame(AdjList)
print(finaldf)
输出:
- ----- ------------------- ------------------- ------------------- -------------------
0 Name Round 1 Round 2 Round 3 Round 4
1 Tom 0.15384615384615385 0.275 0.2222222222222222 0.2625
2 Angus 0.18055555555555555 0.21052631578947367 0.24705882352941178 0.24050632911392406
3 Jack 0.22784810126582278 0.2345679012345679 0.23529411764705882 0.22666666666666666
4 Harry 0.26582278481012656 0.24358974358974358 0.24193548387096775 0.24675324675324675
5 Ben 0.2345679012345679 0.2857142857142857 0.2894736842105263 0.3176470588235294
- ----- ------------------- ------------------- ------------------- -------------------
但是,这种方法似乎效率很低,并且会创建我认为没有必要的临时列表。 有没有更好的方法可以在不使用列表和循环的情况下完成此计算 - 例如 applymap 函数?
使用 pandas 向量化:
>>> columns = ['Round 1', 'Round 2', 'Round 3', 'Round 4']
>>> final = df1[columns] / df2[columns]
您也可以使用filter
>>> df1.filter(like='Round')/df2.filter(like='Round')
可以通过分配取回名称:
>>> final['Name'] = df1['Name']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.