【发布时间】:2019-12-30 21:16:27
【问题描述】:
dataframe = pd.DataFrame({'Date':['This 1A1619 person BL171111 the A-1-24',
'dont Z112 but NOT 1-22-2001',
'mix: 1A25629Q88 or A13B ok'],
'IDs': ['A11','B22','C33'],
})
Date IDs
0 This 1A1619 person BL171111 the A-1-24 A11
1 dont Z112 but NOT 1-22-2001 B22
2 mix: 1A25629Q88 or A13B ok C33
我有上面的数据框。我的目标是替换所有没有连字符的混合单词/数字组合-,例如1A1619I 或 BL171111 或 A13B 但不是 1-22-2001 或 A-1-24 与字母 M。我试图通过identify letter/number combinations using regex and storing in dictionary使用下面的代码
dataframe['MixedNum'] = dataframe['Date'].str.replace(r'(?=.*[a-zA-Z])(\S+\S+\S+)','M')
但我得到了这个输出
Date IDs MixedNum
0 This 1A1619 person BL171111 the A-1-24 A11 M M M M M M M
1 dont Z112 but NOT 1-22-2001 B22 M M M M 1-22-2001
2 mix: 1A25629Q88 or A13B ok C33 M M or M ok
当我真的想要这个输出时
Date IDs MixedNum
0 This 1A1619 person BL171111 the A-1-24 A11 This M person M the A-1-24
1 dont Z112 but NOT 1-22-2001 B22 dont M but NOT 1-22-2001
2 mix: 1A25629Q88 or A13B ok C33 mix: M or M ok
我也尝试了这里建议的正则表达式,但它也对我不起作用 Regex replace mixed number+strings
谁能帮我修改我的正则表达式? r'(?=.*[a-zA-Z])(\S+\S+\S+
【问题讨论】:
标签: python regex python-3.x string pandas