簡體   English   中英

Python 替換字典僅部分產生 output

[英]Python Replacement dictionary is only partially producing an output

我正在嘗試轉換當前這樣的列的 output :

    0        Q122 Your Voice - Energy Group
    1        Q122 Your Voice - Energy Group
    2        Q122 Your Voice - BGSS
    3        Q122 Your Voice - Strategy
    4        Q122 Your Voice - Strategy
    
    10265    Q122 Your Voice - Legal
    10266    Q122 Your Voice - Legal
    10267    Q122 Your Voice - Legal
    10268    Q122 Your Voice - Legal
    10269    Q122 Your Voice - Legal
Name: Survey Name, Length: 10270, dtype: object

下面的代碼檢測到第一個單詞並始終保留它並根據創建的字典替換其他任何內容。

下面的代碼僅生成 output 的f{quarter}部分,但{replacements.get(key, '')}部分未顯示。

代碼: print(df.loc[:, "Survey Name"])

replacements = {
    "Your Voice - Energy Group": "Your Voice - E Group",
    "Your Voice - BGSS": "Your Voice - Services & Solutions",
    "Your Voice - Legal": "Your Voice - LRE",
    "Your Voice - Strategy": "Your Voice - SGBR",
    "Your Voice - Central Storage Line": "Your Voice - CSL"
}



def repl(match, repls=replacements):
    quarter = match.group(1)
    key = " ".join(match.group(2).strip().split())

    return f"{quarter} {replacements.get(key, '')}"


res = df["Survey Name"].str.replace(r"(Q\d+)\s+(.+)", repl, regex=True)
print(res)

output 為:

該代碼僅生成 output 的f{quarter}部分,但{replacements.get(key, '')}部分未顯示。

我得到什么:

    0        Q122 
    1        Q122 
    2        Q122 
    3        Q122 
    4        Q122 
     
    10265    Q122 
    10266    Q122 
    10267    Q122 
    10268    Q122 
    10269    Q122 
    Name: Survey Name, Length: 10270, dtype: object

我應該得到什么:

    0        Q122 Your Voice - E Group
    1        Q122 Your Voice - E Group
    2        Q122 Your Voice - Services & Solutions
    3        Q122 Your Voice - SGBR
    4        Q122 Your Voice - SGBR
     
    10265    Q122 Your Voice - LRECS
    10266    Q122 Your Voice - LRECS
    10267    Q122 Your Voice - LRECS
    10268    Q122 Your Voice - LRECS
    10269    Q122 Your Voice - LRECS
    Name: Survey Name, Length: 10270, dtype: object

什么可能會影響 output 只給出四分之一部分?

更新:

這是在應用代碼更改之前的原始 output。 當我最初發布問題時,我故意更改了其中的一些內容。

0                Q122 Our Voice - BG Energy
1                Q122 Our Voice - BG Energy
2                Q122 Our Voice - BG Energy
3                Q122 Our Voice - BG Energy
4                Q122 Our Voice - BG Energy
               
10265    Q122 Our Voice - Corporate Affairs
10266    Q122 Our Voice - Corporate Affairs
10267    Q122 Our Voice - Corporate Affairs
10268    Q122 Our Voice - Corporate Affairs
10269    Q122 Our Voice - Corporate Affairs
Name: Survey Name, Length: 10270, dtype: object

改用這個repl function:

def repl(match, repls=replacements):
    quarter = match.group(1)
    key = " ".join(match.group(2).strip().split())

    return f"{quarter} {replacements.get(key, key)}"

注意變化,現在是這一行:

return f"{quarter} {replacements.get(key, '')}"

是:

return f"{quarter} {replacements.get(key, key)}" # '' was changed by key

Output

0             Q122 Our Voice - BG Energy
1             Q122 Our Voice - BG Energy
2             Q122 Our Voice - BG Energy
3             Q122 Our Voice - BG Energy
4             Q122 Our Voice - BG Energy
5     Q122 Our Voice - Corporate Affairs
6     Q122 Our Voice - Corporate Affairs
7     Q122 Our Voice - Corporate Affairs
8     Q122 Our Voice - Corporate Affairs
9     Q122 Our Voice - Corporate Affairs
10                 Q122 Your Voice - LRE
11                 Q122 Your Voice - LRE
12                 Q122 Your Voice - LRE
13                 Q122 Your Voice - LRE
14                 Q122 Your Voice - LRE
Name: Survey Name, dtype: object

輸入

上面的 output 是用這個 DataFrame 生成的:

{'Survey Name': {0: 'Q122 Our Voice - BG Energy',
                 1: 'Q122 Our Voice - BG Energy',
                 2: 'Q122 Our Voice - BG Energy',
                 3: 'Q122 Our Voice - BG Energy',
                 4: 'Q122 Our Voice - BG Energy',
                 5: 'Q122 Our Voice - Corporate Affairs',
                 6: 'Q122 Our Voice - Corporate Affairs',
                 7: 'Q122 Our Voice - Corporate Affairs',
                 8: 'Q122 Our Voice - Corporate Affairs',
                 9: 'Q122 Our Voice - Corporate Affairs',
                 10: 'Q122 Your Voice - Legal',
                 11: 'Q122 Your Voice - Legal',
                 12: 'Q122 Your Voice - Legal',
                 13: 'Q122 Your Voice - Legal',
                 14: 'Q122 Your Voice - Legal'}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM