Replace Numbers In A String With A String And The Length Of The Number Appended To It
Basically I need to take a column in a dataframe which has a combination of chars and numbers, like 'XYZABC/123441 s sdx' and such similar types I need to remove all punctuation,
Solution 1:
You may use apply
like
defrepl(x):
return re.sub(r'\d+', lambda m: "NUMB{}".format(len(m.group())), x)
df['colname'] = df['colname'].apply(repl)
Or to use the same logic as in your code, replace x = re.sub(r"\d+", "NUMB", str(x))
with
x = re.sub(r'\d+', lambda m: "NUMB{}".format(len(m.group())), x)
The re.sub(r'\d+', lambda m: "NUMB{}".format(len(m.group())), x)
will find any non-overlapping digit chunks and will replace them with NUMB
and the length of the digit chunk.
Post a Comment for "Replace Numbers In A String With A String And The Length Of The Number Appended To It"