[英]Replace part of string in column DataFrame
问题:目前,我有一列 dataframe 的结果如1.Goalkeeper , 4.Midfield ...我无法更改部分替换字符串。
目标:我的目标是用1.GK , 4.MD替换它......但它不会进行替换。 好像这些行没有写。 有任何想法吗?
如果输入与替换相同,则代码有效。 例如,守门员,中场......但是当我用(数字+点)作为前缀时它不起作用。
代码
df2['Posicion'].replace({'Goalkeeper':'GK','Left-Back':'LI','Defensive Midfield':'MCD'
,'Right Midfield':'MD','Attacking Midfield':'MP','Right Winger':'ED','Centre-Forward':'DC',
'Centre-Back':'DFC','Right-Back':'LD','Central Midfield':'MC','Second Striker':'SD',
'Left Midfield':'MI','Left Winger':'EI','N':'','None':'','Sweeper':'DFC'}, inplace=True)
regex=True
将在这里解决问题。
df2 = pd.DataFrame({
'Posicion' : ['1.Goalkeeper', '2.Midfield', '3.Left Winger']
})
df2['Posicion'].replace({'Goalkeeper':'GK',
'Left Winger':'EI',
'N':'',
'None':'',
'Sweeper':'DFC'},
regex=True,
inplace=True)
Output:
Posicion
0 1.GK
1 2.Midfield
2 3.EI
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.