【问题标题】:Replace first few digits birthdate pandas替换前几位生日熊猫
【发布时间】:2019-11-28 22:56:39
【问题描述】:

背景

我有以下示例 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'Date of Birth: ^\d{3}-\d{2}', "*BDAY*", regex=True) 但它并没有给我想要的输出

问题

如何实现我想要的输出?

【问题讨论】:

    标签: regex python-3.x string pandas replace


    【解决方案1】:

    这个表达式也可能有效:

    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= df.replace(r'(?i)date\s+of\s+birth:\s+\d{1,2}/\d{1,2}/', "Date of Birth: *BDAY*", regex=True)
    
    print(df)
    

    表达式在regex101.com 的右上方面板中进行了解释,如果您想探索/简化/修改它,在this link 中,您可以查看它如何与一些示例输入进行匹配,如果您愿意的话。

    【讨论】:

      【解决方案2】:

      试试这个:

      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
      

      【讨论】:

        【解决方案3】:

        错误的正则表达式

        您的正则表达式查找 3 位数字,然后是“-”,然后是 2 位数字。您的示例数据有 2 位数字,一个“/”,然后是 2 位数字。

        试试:

        df.replace(
            r'(Date of Birth:\s+)\d{2}/\d{2}/',
            r"\1*BDAY*",
            regex=True)
        

        【讨论】:

        • 我认为这很接近。 One *BDAY*/2001 last here 已正确识别。但是This person was born Date of Birth: 5/6/1950 错过了。
        • 我调整了正则表达式模式和替换。 PS。当我测试你的正则表达式时,它没有替换任何东西。
        猜你喜欢
        • 1970-01-01
        • 2017-08-15
        • 2023-03-06
        • 2023-03-09
        • 2016-01-20
        • 1970-01-01
        • 1970-01-01
        • 2020-03-21
        • 2020-09-07
        相关资源
        最近更新 更多