在 pandas 数据框中去除左括号和斜线的字符串

Stripping strings of left brackets and slashes in pandas dataframe

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


        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())

但是,当我添加“/”时,它并没有删除斜杠... 我试过了

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

谢谢。

单独解析条件是否有效:

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())