import pandas as pd
df = pd.DataFrame({'col1': ['asd12_01'] * 5})
def increment_string(s, i):
prefix, num = s.rsplit('_', 1)
return f"{prefix}_{int(num) + i:02d}"
df['col1'] = df.apply(lambda x: increment_string(x['col1'], x.name), axis=1)
print(df)
The above code creates a sample dataframe
with a column col1 filled with the same string 'asd12_01' on all rows. It then defines a function increment_string that takes in a string s and an integer i, and returns a new string where the number after the last underscore is incremented by i. The function is then applied to each row of the dataframe
using the apply method, with x.name representing the row index. The resulting dataframe
will have the col1 column incremented by 1 for each row, as shown below:
col1
0 asd12_01
1 asd12_02
2 asd12_03
3 asd12_04
4 asd12_05