import pandas as pd
dataframe = pd.DataFrame({'Data' : ['The **ALI**1929 for 90 days but not 77731929 ',
'For all **ALI**1952 28A 177945 ',
'But the **ALI**1914 and **ALI**1903 1912',],
'ID': [1,2,3]
})
Data ID
0 The **ALI**1929 for 90 days but not 77731929 1
1 For all **ALI**1952 28A 177945 2
2 But the **ALI**1914 and **ALI**1903 1912 3
我的数据框看起来像我上面的。我的目标是用与相关联的OLDER任何数字替换这个词。将是,也将是,但将保持不变。从如何从python中的字符串中提取一定长度的数字?我努力了1929**ALI****ALI**1929**ALI**OLDERALI**1903**ALI**OLDER**ALI**1952
dataframe['older'] = dataframe['Data'].str.replace(r'(?<!\d)(\d{3})(?!\d)', 'OLDER')
但这对我想要的效果不太好。我想要这样的东西作为输出
Data ID older
0 The ALI**OLDER for 90 days but not 77731929
1 For all ALI**1952 28A 177945
2 But the ALI**OLDER and ALI**OLDER 1912
我如何更改我的正则表达式str.replace(r'(?<!\d)(\d{3})(?!\d)'来做到这一点?