我有兩個熊貓資料框,想應用一個函式“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 函式?
uj5u.com熱心網友回復:
使用 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']
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/489747.html
