【问题标题】:Using regex to map values in dataframe using python使用正则表达式使用 python 映射数据框中的值
【发布时间】:2021-01-27 17:35:35
【问题描述】:

有一个带有值的df:


name   number     addition

tom    123        this is tom , number is [[number]]
mark   1234       this is mark , number is [[number]]

使用正则表达式如何将数字列中的值与 [[number]] 以及这样的列进行映射


name   number     addition

tom    123        this is tom , number is 123
mark   1234       this is mark , number is 1234

【问题讨论】:

    标签: python regex pandas dataframe mapping


    【解决方案1】:

    你可以使用apply:

    df['addition'] =  df.apply(lambda row: row['addition'].replace('[[number]]', str(row['number'])), axis=1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-01
      • 1970-01-01
      • 2020-09-02
      • 2020-07-03
      • 2020-09-09
      • 2016-09-30
      相关资源
      最近更新 更多