如何从数据框中的字典中删除单词

时间:2020-06-15 12:21:07

标签: python regex dataframe

我正在尝试从数据框列中删除某些单词,但失败了...

一些示例数据:

    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  

由于某些原因,省略了括号。我尝试将'()'添加到我的替换值字典中,但似乎没有任何作用。

1 个答案:

答案 0 :(得分:0)

您应该使用括号:

intent.putextras()

由于Option Explicit Public Sub CommandButton1_Click() Dim myArray() As Variant Dim xrow As Long, xcol As Long Dim rowIndex As Long, colIndex As Long, i As Long, j As Long rowIndex = 0 colIndex = 0 xrow = 1 xcol = 1 'outer loop down rows Do Until Cells(xrow, xcol).Value = "" 'inner loop across columns Do Until Cells(xrow, xcol).Value = "" myArray(rowIndex, colIndex) = Cells(xrow, xcol).Value colIndex = colIndex + 1 ' increase array in 2nd dimension xcol = xcol + 1 'increase column on worksheet Loop xcol = 0 'reset after finishing row loop colIndex = 0 'reset 2nd dimension index in array xrow = xrow + 1 'increase row on worksheet rowIndex = rowIndex + 1 ' increase array in 1st dimension Loop End Sub 表示单词边界,因此应将其移到括号内。否则它将与您的文本不匹配。