删除数据框中的正则表达式、方括号、单引号和双引号

2024-05-16 14:16:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试删除正则表达式、方括号、单引号和双引号,并将其替换为空字符串。我做得不对。 输入如下所示:

Accident_type                      Injury_classification        
                          
['Strike fixed/station obj']     ["Assault in PI Cases", 'Other Injuries']
['Slip, trip, fall']             ["Work Related Injury", 'Other Injuries']
etc

我尝试了df['Injury_classification'].str.replace(r" \(.*\)",""),但它没有删除任何内容。代码运行了,但结果相同,没有删除任何内容

然后我试着

df['Injury_classification'] = pd.DataFrame([str(line).strip('[').strip(']').strip('\'').strip('\'').strip('"') for line in df['Injury_classification']])

电流输出:

Accident_type                      Injury_classification      
                                 
empty                       Assault in PI Cases", 'Other Injuries
empty                       Work Related Injury", 'Other Injuries
etc

正如您所看到的,仍然有一些单引号,有时也有双引号。我想知道如何处理这件事?我有大约20-30根类似结构的柱子。现在,我正在为同一个命令逐行运行,但对于那么多的列来说,这并没有效率。我想知道如何编写一个循环来删除所有列的正则表达式、单引号和双引号

预期输出:

Accident_type                      Injury_classification      
                                 
Strike fixed/station obj    Assault in PI Cases, Other Injuries
Slip, trip, fall            Work Related Injury, Other Injuries
etc

谢谢


Tags: intypepiworkstripclassificationotherrelated