我拥有的数据集包含NHL播放器数据,看起来像这样(简化):
teamNAME playerID gameID metric Won/Lost
CAP 8473345 20 10 1
WILD 8475467 20 10 0
NY 8471345 21 10 1
STARS 8475756 21 10 0
我想做的是在所有行中都有对手teamNAME,这样看起来像这样:
teamNAME playerID gameID metric Won/Lost Opponent
CAP 8473345 20 10 1 WILD
WILD 8475467 20 10 0 CAP
NY 8471345 21 10 1 STARS
STARS 8475756 21 10 0 NY
您如何解决这个问题?
答案 0 :(得分:3)
您可以GroupBy
gameID
并结合使用transform
和lambda
函数来旋转值:
df['Opponent'] = df.groupby('gameID').teamNAME.transform(lambda x: x[::-1])
teamNAME playerID gameID metric Won/Lost Opponent
0 CAP 8473345 20 10 1 WILD
1 WILD 8475467 20 10 0 CAP
2 NY 8471345 21 10 1 STARS
3 STARS 8475756 21 10 0 NY
答案 1 :(得分:1)
从SQL角度解决此问题,您可以对gameID执行交叉联接,然后对teamNAME进行过滤:
(df.merge(df[['teamNAME', 'gameID']], on='gameID', how='left')
.query('teamNAME_x != teamNAME_y')
.rename({'teamNAME_x': 'teamNAME', 'teamNAME_y': 'Opponent'}, axis=1))
teamNAME playerID gameID metric Won/Lost Opponent
1 CAP 8473345 20 10 1 WILD
2 WILD 8475467 20 10 0 CAP
5 NY 8471345 21 10 1 STARS
6 STARS 8475756 21 10 0 NY
答案 2 :(得分:1)
from itertools import chain
grouper = df.groupby('gameID')['teamNAME']
df['Opponent'] = list(chain.from_iterable(x.values[::-1] for _, x in grouper))
print(df)
teamNAME playerID gameID metric Won/Lost Opponent
0 CAP 8473345 20 10 1 WILD
1 WILD 8475467 20 10 0 CAP
2 NY 8471345 21 10 1 STARS
3 STARS 8475756 21 10 0 NY