繁体   English   中英

如果同一 pandas 列中的部分字符串匹配,则更新另一列中的值

[英]If partial string in the same pandas column match then update the value in another column

我有来自这个 CSV 文件的 pandas 数据框。 CSV

我有几个这些文件,我将它们组合成一个大文件。 我需要遍历播放器列,如果一个单元格部分匹配对应的 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM