You may use
df['column'] = df['column'].str.replace(r'^(C3-)(\d)$', r'\g<1>000\2')
See the regex demo. If C
can be any uppercase ASCII letter, replace it with [A-Z]
.
Or, a bit more generic for 1-3 digit numbers:
df['column'] = df['column'].str.replace(r'^(C3-)(\d{1,3})$', lambda x: "{}{}".format(x.group(1), x.group(2).zfill(4)))
Details
^
- start
(C3-)
- Group 1: C3-
(\d)
- Group 2: a digit (\d{1,3}
matches 1 to 3 digits)
$
- end of string
\g<1>
- value of Group 1
000
- three zeros
\2
- value of Group 2
A Python test:
import pandas as pd
df = pd.DataFrame({'column': ['C3-1', 'C3-12', 'C3-123', 'C3-1234']})
df['column'] = df['column'].str.replace(r'^(C3-)(\d{1,3})$', lambda x: "{}{}".format(x.group(1), x.group(2).zfill(4)))
Output:
>>> df
column
0 C3-0001
1 C3-0012
2 C3-0123
3 C3-1234