Home > Back-end >  Using Regex in Pandas to change case of captured string
Using Regex in Pandas to change case of captured string

Time:08-17

I'm trying to capitalize the first letter after a parantheses in a dataframe column.

This is the closest I got but I don't know how to actually reference the string that is captured so I can apply .upper() method to it

df['item_name'] = df['item_name'].str.replace(r"(?<=[(])[a-z]", ###what to put?###.upper(), regex=True)

CodePudding user response:

You ca pass a function as replacement value:

df['item_name'] = df['item_name'].str.replace(r"(?<=[(])[a-z]",
                                              lambda m: m.group().upper(),
                                              regex=True)

Example: vowels to uppercase.

df = pd.DataFrame({'A': ['abcde', 'fghij']})
df['B'] = df['A'].str.replace(r"[aeiou]", lambda m: m.group().upper(), regex=True)

output:

       A      B
0  abcde  AbcdE
1  fghij  fghIj
  • Related