With pandas.DataFrame.replace in python how to replace all ä with ae only the ones that are in between ${}?
Below is my Python code that I tried with but it didn't worked:
df.replace({'Desc': r'\$\{.*ä\}'} , {'Desc': r'\$\{.*ae\}'}, regex=True)
- As a first e.g.
Actual Result: Lorem Ipsum is ä simply dummy text ${Männer} Lorem Ipsum is simply dummy text ä.
Expected Result: Lorem Ipsum is ä simply dummy text ${Maenner} Lorem Ipsum is simply dummy text ä.
- As a second e.g. where ä is occurring multiple time
Actual Result: Lorem Ipsum ${flächenmäßig} is ä simply dämmy ${flächenmäßig} text
Expected Result: Lorem Ipsum ${flaechenmaeßig} is ä simply dämmy ${flaechenmaeßig} text
Please note that the examples I had mentioned do not have any meaning.
CodePudding user response:
Here's a solution:
import re
rgx = re.compile(r'(\$\{. ?\})')
df['Desc'] = df['Desc'].apply(lambda s: [s := s.replace(rep, rep.replace('ä', 'ae'), 1) for rep in rgx.findall(s)][-1])
Output:
Desc
0 Lorem Ipsum is ä simply dummy text ${Maenner} Lorem Ipsum is simply dummy text ä.
1 Lorem Ipsum ${flaechenmaeßig} is ä simply dämmy ${flaechenmaeßig} text