You can use Series.str.replace
with a backreference to the capturing group and have it be preceded by 0
(see also: re.sub
):
data = {'sl.no': {0: 321, 1: 324, 2: 326, 3: 328, 4: 330, 5: 330, 6: 331},
'data_col1': {0: 'abc-1', 1: 'abc-2', 2: 'abc-3', 3: 'abc-4',
4: 'abc-5', 5: 'abc-12', 6: 'xyz-1'}}
df = pd.DataFrame(data)
df['data_col1'] = df['data_col1'].str.replace(r'(?<=abc-)(\d)$',r'0\1', regex=True)
df
sl.no data_col1
0 321 abc-01
1 324 abc-02
2 326 abc-03
3 328 abc-04
4 330 abc-05
5 330 abc-12
6 331 xyz-1
For an explanation of the patterns, see here.
Alternatively, as mentioned by @mozway in the comments, you could pass a function to repl
and apply str.zfill
:
df['data_col1'] = df['data_col1'].str.replace(r'(?<=abc-)(\d+)$',
lambda x: x.group().zfill(2),
regex=True)
A more labored alternative: use Series.str.split
with Series.str.zfill
and do something like this:
tmp = df['data_col1'].str.split('-', expand=True)
df['data_col1'] = tmp[0] + '-' + np.where(tmp[0].eq('abc'), tmp[1].str.zfill(2), tmp[1])