【发布时间】:2020-10-04 20:37:41
【问题描述】:
我正在尝试从数据框列中删除某些单词并且失败得很惨......
我的一些示例数据:
Stock_Name
Vanguard US Government Bond Index GBP Inc (Hedged)
Vanguard US Government Bond Index GBP Acc (Hedged)
Vanguard US Government Bond Index GBP Inc
Vanguard US Government Bond Index USD Acc
字典:
replace_values = {
r'\bAcc\b': "",
r'\bInc\b': "",
r'\b(Hedged)\b': "",
r'\bGBP\b': "",
r'\bUSD\b': ""
}
df["Stock_Name"] = df["Stock_Name"].replace(replace_values,regex=True)
我得到的输出:
Vanguard US Government Bond Index ()
Vanguard US Government Bond Index ()
Vanguard US Government Bond Index
Vanguard US Government Bond Index
由于某种原因,括号被省略了。我尝试在我的替换值字典中添加“()”,但它似乎没有做任何事情。
【问题讨论】:
-
要匹配
(,请在模式中使用\(。与)和其他special regex metacharacters 相同。