have a df with values
0 1
0 RS 125 | password1
1 EURO 25 | password2
2 DNR 30 | password3
Using regex how to change values of these at once likes the values after RS,EURO,DNR should be replace with 100
output should be
0 1
0 RS 100 | password1
1 EURO 100 | password2
2 DNR 100 | password3
Tried this
def Currency(0):
pattern = re.compile("(Rs |EURO |DNR |)")
result = pattern.match(name)
Trying to replace the matched ones.
You may use
df['col'] = df['col'].str.replace(r'(?i)((?:Rs|EURO|DNR)\s*)\d+', r'\g<1>100')
Or, if you want to match the currency names as whole words, prepend the capturing group with a word boundary \b:
r'(?i)\b((?:Rs|EURO|DNR)\s*)\d+'
^^
See this regex demo.
Details
(?i) - case insensitive flag((?:Rs|EURO|DNR)\s*) - Group 1 (\g<1> in the replacement pattern refers to this value):
(?:Rs|EURO|DNR) - Rs or EURO or DNR\s* - 0+ whitespaces\d+ - one or more digitsSee the regex demo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With