python - 删除 Pandas DataFrame 中的正则表达式、方括号、单引号和双引号
问题描述
我正在尝试删除正则表达式、方括号、单引号和双引号并将其替换为空字符串。我做得不对。 输入如下:
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
谢谢
解决方案
我会在str.replace
这里使用一个字符类:
df['Injury_classification'] = df['Injury_classification'].str.replace("[\[\]\"']", "")
这将输入['Slip', 'trip', "fall"]
到Slip, trip fall
.
推荐阅读
- wireshark - Wireshark解剖器:格式化纪元时间
- javascript - 否则即使控制台日志识别出具有长度的 if 语句也会运行
- javascript - 如何根据键名合并 2 个数组并根据合并后的值进行排序?
- javascript - 合并对象数组,修改一些结果条目
- javascript - Xrm.Page.getControl() 在 Dynamics 365 Sales Hub 中不起作用
- regex - 尝试不使用 RegEx 否定后向匹配日语单词
- c# - 试图从 bat 文件中读取,但它给了我一个错误
- tensorflow - 在 TensorFlow 中将 2D 卷积转换为 1D 卷积 + 仿射变换?
- android - 我应该同时更改目标 SDK 和编译 SDK 以支持更高版本吗?
- eloquent - 如何在 laravel / eloquent 中为 mysql 8 做公用表表达式?