我有两个DataFrame。下面是每个数据框的两个摘要。
此数据框称为外观。
yearID teamID playerID GS
yearID teamID
1994 NYA 1994 NYA abbotji01 24.0
FLO 1994 FLO abbotku01 93.0
OAK 1994 OAK acrema01 0.0
MIN 1994 MIN aguilri01 0.0
OAK 1994 OAK aldremi01 39.0
SLN 1994 SLN alicelu01 48.0
TOR 1994 TOR alomaro01 102.0
CLE 1994 CLE alomasa02 78.0
MON 1994 MON aloumo01 106.0
CHA 1994 CHA alvarwi01 24.0
SEA 1994 SEA amarari01 53.0
CLE 1994 CLE amaroru02 1.0
BAL 1994 BAL anderbr01 109.0
CAL 1994 CAL anderbr02 18.0
CAL 1994 CAL anderga01 3.0
PHI 1994 PHI anderla02 0.0
SEA 1994 SEA anthoer01 66.0
KCA 1994 KCA appieke01 23.0
FLO 1994 FLO aquinlu01 1.0
FLO 1994 FLO ariasal01 22.0
TEX 1994 TEX armstja01 2.0
SLN 1994 SLN arochre01 7.0
SDN 1994 SDN ashbyan01 24.0
LAN 1994 LAN ashlebi01 2.0
CHA 1994 CHA assenpa01 0.0
LAN 1994 LAN astacpe01 23.0
NYA 1994 NYA ausanjo01 0.0
SDN 1994 SDN ausmubr01 94.0
ATL 1994 ATL averyst01 24.0
SEA 1994 SEA ayalabo01 0.0
... ... ... ...
2018 MIA 2018 MIA wittgni01 0.0
COL 2018 COL wolteto01 56.0
SLN 2018 SLN wongko01 98.0
LAN 2018 LAN woodal02 27.0
LAA 2018 LAA woodbl01 0.0
TBA 2018 TBA woodhu01 8.0
MIL 2018 MIL woodrbr01 4.0
BOS 2018 BOS workmbr01 0.0
NYN 2018 NYN wrighda03 1.0
ATL 2018 ATL wrighky01 0.0
BAL 2018 BAL wrighmi01 2.0
BOS 2018 BOS wrighst01 4.0
BAL 2018 BAL wynnsau01 33.0
BAL 2018 BAL yacabji01 7.0
TBA 2018 TBA yarbrry01 6.0
SDN 2018 SDN yateski01 0.0
MIL 2018 MIL yelicch01 144.0
LAA 2018 LAA youngch04 32.0
LAA 2018 LAA younger03 30.0
CHN 2018 CHN zagunma01 0.0
MIL 2018 MIL zagurmi01 0.0
NYN 2018 NYN zamorda01 0.0
CHN 2018 CHN zastrro01 0.0
ARI 2018 ARI zieglbr01 0.0
MIA 2018 MIA zieglbr01 0.0
CLE 2018 CLE zimmebr01 29.0
DET 2018 DET zimmejo02 25.0
WAS 2018 WAS zimmery01 72.0
CHN 2018 CHN zobribe01 108.0
SEA 2018 SEA zuninmi01 102.0
[33967 rows x 4 columns]
此DataFrame称为Fielding。
yearID teamID playerID POS G GS DRAR
yearID teamID
1994 FLO 1994 FLO abbotku01 SS 99 93.0 4.340000
OAK 1994 OAK aldremi01 1B 27 20.0 -1.266667
OAK 1994 OAK aldremi01 OF 35 18.0 1738.000000
SLN 1994 SLN alicelu01 2B 53 48.0 0.960000
SLN 1994 SLN alicelu01 OF 2 0.0 1738.000000
TOR 1994 TOR alomaro01 2B 106 102.0 2.040000
CLE 1994 CLE alomasa02 C 78 78.0 4.680000
MON 1994 MON aloumo01 OF 106 106.0 1738.000000
SEA 1994 SEA amarari01 1B 2 0.0 -0.000000
SEA 1994 SEA amarari01 2B 42 37.0 0.740000
SEA 1994 SEA amarari01 OF 16 7.0 1738.000000
SEA 1994 SEA amarari01 SS 7 5.0 0.233333
CLE 1994 CLE amaroru02 OF 12 0.0 1738.000000
BAL 1994 BAL anderbr01 OF 109 109.0 1738.000000
CAL 1994 CAL anderga01 OF 4 3.0 1738.000000
SEA 1994 SEA anthoer01 OF 71 65.0 1738.000000
FLO 1994 FLO ariasal01 3B 15 4.0 0.053333
FLO 1994 FLO ariasal01 SS 20 18.0 0.840000
LAN 1994 LAN ashlebi01 OF 2 2.0 1738.000000
SDN 1994 SDN ausmubr01 1B 1 0.0 -0.000000
SDN 1994 SDN ausmubr01 C 99 94.0 5.640000
CLE 1994 CLE baergca01 2B 102 102.0 2.040000
HOU 1994 HOU bagweje01 1B 109 107.0 -6.776667
HOU 1994 HOU bagweje01 OF 1 1.0 1738.000000
FLO 1994 FLO barbebr01 2B 106 98.0 1.960000
DET 1994 DET barnesk01 1B 15 0.0 -0.000000
DET 1994 DET barnesk01 OF 4 1.0 1738.000000
HOU 1994 HOU basske01 OF 57 48.0 1738.000000
PHI 1994 PHI batiski01 3B 42 36.0 0.480000
PHI 1994 PHI batiski01 SS 17 14.0 0.653333
... ... ... .. ... ... ...
2018 HOU 2018 HOU whitety01 1B 42 33.0 -2.090000
WAS 2018 WAS wietema01 C 73 64.0 3.840000
BAL 2018 BAL wilkest01 2B 9 8.0 0.160000
BAL 2018 BAL wilkest01 3B 6 4.0 0.053333
BAL 2018 BAL wilkest01 SS 1 0.0 0.000000
TBA 2018 TBA williju02 OF 1 0.0 1738.000000
CIN 2018 CIN willima07 OF 40 27.0 1738.000000
SFN 2018 SFN willima08 OF 27 27.0 1738.000000
PHI 2018 PHI willini01 OF 106 101.0 1738.000000
MIN 2018 MIN wilsobo02 C 47 45.0 2.700000
CIN 2018 CIN winkeje01 OF 76 71.0 1738.000000
SLN 2018 SLN wisdopa01 1B 4 2.0 -0.126667
SLN 2018 SLN wisdopa01 3B 13 6.0 0.080000
COL 2018 COL wolteto01 2B 2 0.0 0.000000
COL 2018 COL wolteto01 3B 1 0.0 0.000000
COL 2018 COL wolteto01 C 64 56.0 3.360000
COL 2018 COL wolteto01 OF 2 0.0 1738.000000
SLN 2018 SLN wongko01 2B 119 98.0 1.960000
NYN 2018 NYN wrighda03 3B 1 1.0 0.013333
BAL 2018 BAL wynnsau01 C 41 33.0 1.980000
MIL 2018 MIL yelicch01 OF 145 144.0 1738.000000
LAA 2018 LAA youngch04 OF 48 31.0 1738.000000
LAA 2018 LAA younger03 OF 37 30.0 1738.000000
CHN 2018 CHN zagunma01 OF 1 0.0 1738.000000
CLE 2018 CLE zimmebr01 OF 34 29.0 1738.000000
WAS 2018 WAS zimmery01 1B 73 72.0 -4.560000
CHN 2018 CHN zobribe01 1B 4 3.0 -0.190000
CHN 2018 CHN zobribe01 2B 63 42.0 0.840000
CHN 2018 CHN zobribe01 OF 84 62.0 1738.000000
SEA 2018 SEA zuninmi01 C 111 101.0 6.060000
[26608 rows x 7 columns]
这两个数据帧的长度不同;但是,两个DataFrame中的许多playerID都是相同的。我想通过具有相同玩家ID的数据帧中的每个项目,从外观的GS值减去Fielding的GS值。例如,如果我具有玩家ID zuninmi01,则我想创建一个新数据框,其zuninmu01等于1.0,因为102.0-101 = 1。 我想创建一个循环,为外观中的每个玩家ID执行此操作。知道如何做到这一点吗?请注意,某些玩家ID在Fielding中重复了两次,因此在这种情况下,我想从Appearances中将字段中具有相同玩家值的所有PlayerID的GS值减去。
我尝试了下面的代码,但是没有用。
DHlst = []
for i in range(0, len(Appearances)):
DH = (Appearances['GS'][i]) - Fielding['GS'][Appearances['playerID'][i]]
DHlst.append(DH)