regex - 替换 Pandas 出生日期的前几位

标签 regex python-3.x string pandas replace

背景

我有以下示例 df

import pandas as pd
df = pd.DataFrame({'Birthdate':['This person was born Date of Birth: 5/6/1950 and other',
                          'no Date of Birth: nothing here',
                          'One Date of Birth: 01/01/2001 last here'], 
                  'P_ID': [1,2,3],
                  'N_ID' : ['A1', 'A2', 'A3']} 

                 )

 df
                                 Birthdate                 N_ID P_ID
    0   This person was born Date of Birth: 5/6/1950 a...   A1  1
    1   no Date of Birth: nothing here                      A2  2
    2   One Date of Birth: 01/01/2001 last here             A3  3

目标

将生日的前几位数字替换为 *BDAY*,例如5/6/1950 变为 *BDAY*1950

所需输出

                                 Birthdate                 N_ID P_ID
    0   This person was born Date of Birth: *BDAY*1950 a... A1  1
    1   no Date of Birth: nothing here                      A2  2
    2   One last Date of Birth: *BDAY*2001 last here        A3  3

尝试过

来自python - Replace first five characters in a column with asterisks我尝试过以下代码: df.replace(r'出生日期: ^\d{3}-\d{2}', "*BDAY*", regex=True) 但它并没有完全给我我想要的输出

问题

如何实现我想要的输出?

最佳答案

试试这个:

df['Birthdate'] = df.Birthdate.str.replace(r'[0-9]?[0-9]/[0-9]?[0-9]/', '*BDAY*')


Out[273]:
                                           Birthdate  P_ID N_ID
0  This person was born Date of Birth: *BDAY*1950...     1   A1
1                     no Date of Birth: nothing here     2   A2
2            One Date of Birth: *BDAY*2001 last here     3   A3

关于regex - 替换 Pandas 出生日期的前几位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57121057/

相关文章:

c# - 正则表达式在斜线之间得到一个词

python-3.x - 如何使用 MacPorts 运行 Webots 示例?

Python 3 奇怪的元类行为

django - 在 Django 的 trans 方法中转义引号

c++ - wcscat_s 函数 - 缓冲区错误

python - 正则表达式单行仅匹配特定单词后的内容?

php - 如何在每个电子邮件地址中添加单引号或双引号

php - 正则表达式字符串中断

php - 如何使用 php preg_replace 替换 HTML 标签

python - 如何在不使用OpenCV库的情况下在Python中读取多 channel 16位/ channel 图像?