[英]If partial string in the same pandas column match then update the value in another column
我有幾個這些文件,我將它們組合成一個大文件。 我需要遍歷播放器列,如果一個單元格部分匹配對應的 CPTN 單元格,那么我需要將包含 CPTN 的播放器單元格的 Pos 值更新為 CPTN + '(POS)'。
最終結果將如下所示:
Bryce Mitchell WR
Bryant Koback RB
Bryant Koback CPTN CPTN (RB)
Bryce Mitchell CPTN CPTN (WR)
這是一個要測試的電子表格: 測試表
一個想法是通過掩碼進行更新:
cptn_mask = df['Player'].str.contains('CPTN')
df.loc[cptn_mask , 'Player'] = "CPTN" + df.loc[cptn_mask , 'Position']
我稍微調整了數據,這樣我們就可以看到沒有玩CPTN的人
df
###
Player Pos Salary
0 Bryce Mitchell WR 6400
1 Bryant Koback RB 10200
2 Bryant Koback CPTN CPTN 15300
3 Bryce Mitchell CPTN CPTN 9600
4 Jordan Legendre QB 23450
temp = df.copy()
temp['Player'] = temp['Player'].str.replace(' CPTN', '')
temp_g = temp.groupby('Player')['Pos'].apply(lambda x: x.str.cat(sep=' ')).reset_index()
temp_g['Player'] = np.where(temp_g['Pos'].str.contains('CPTN'), temp_g['Player'] + ' CPTN', temp_g['Player'])
temp_g['Pos'] = np.where(temp_g['Pos'].str.contains('CPTN'), 'CPTN (' + temp_g['Pos'].str.replace('CPTN', '').str.strip() + ')', temp_g['Pos'])
temp_g = temp_g[temp_g['Player'].str.contains('CPTN')]
df['Pos'] = np.where(df['Player'].str.contains('CPTN'), df['Player'].map(temp_g.set_index('Player')['Pos']), df['Pos'])
df
###
Player Pos Salary
0 Bryce Mitchell WR 6400
1 Bryant Koback RB 10200
2 Bryant Koback CPTN CPTN (RB) 15300
3 Bryce Mitchell CPTN CPTN (WR) 9600
4 Jordan Legendre QB 23450
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.