删除熊猫数据框中的左括号和斜杠字符串

时间:2020-07-03 19:53:36

标签: pandas string dataframe strip

我需要去除第三个'-'之后,第一个“(”之后,第一个'/'之后的字符,并将结果保留在新列keepcat中。

async DeleteRole(req, res, next) {
  let validationData = await this.ValidationAction(req, res);
  if (validationData[0]) {
    Role.findByIdAndUpdate(req.params.id, {
      $set: { isDelete: true }
    }, { useFindAndModify: false }, (error, role)=> {
      if (error) {
        next(error);
      } else if (!role) {
        res.status(200).send({
          message: "NotFoundRecord",
          statusCode: 200,
          success: false,
        });
      } else {
        res.status(200).send({
          message: "Success",
          statusCode: 200,
          success: true,
        })
      }
    });
  } else {
    res.status(200).send({
      message: "BadREquest",
      statusCode: 400,
      success: false,
    });
  }
}

我设法用以下方式删除破折号(“-”)和方括号(“(”)):


        violation_code        violation_description                                 keepcat
ticket_id           
22056   9-1-36(a)             Failure of owner to obtain certificate of compliance  9-1-36
27586   61-63.0600            Failed To Secure Permit For Lawful Use Of Building    61-63.0600
18738   61-63.0500            Failed To Secure Permit For Lawful Use Of Land        61-63.0500
18735   61-63.0100            Noncompliance/Grant Condition/BZA/BSE                 61-63.0100
23812   61-81.0100/32.0066    Open Storage/ Residential/ Vehicles                   61-81.0100/32.0066
26686   61-130.0000/130.0300  Banner/ Signage/ Antenna                              61-130.0000/130.0300
325555  9-1-43(a) - (Structu  Fail to comply with an Emergency                      9-1-43 

但是,当我添加“ /”时,它不会删除斜杠... 我尝试过

df['keepcat']=df['violation_code'].apply(lambda x: "-".join(x.split("-")[:3]) and x.split('(')[0].strip())

谢谢。

1 个答案:

答案 0 :(得分:1)

如果分别解析条件,它是否起作用:

df['keepcat'] = df['violation_code'].apply(lambda x: "-".join(x.split("-")[:3]))
df['keepcat'] = df['keepcat'].apply(lambda x: x.split('(')[0].strip())
df['keepcat'] = df['keepcat'].apply(lambda x: x.split('/')[0].strip())