0
votes

I am trying to transform the output of a column which is currently like this:

    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

The code below detects the first word and always keeps it and replaces anything else accordingly to the dictionary created.

The code below is produces only the f{quarter} part of the output but the {replacements.get(key, '')} part is not showing.

The code: 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)

with the output being:

The code produces only the f{quarter} part of the output but the {replacements.get(key, '')} part is not showing.

What I get:

    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

What I should be getting:

    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

What could be affecting the output to only give the quarter part?