How to increase alphanumeric column values by 1 increment in Python?

Viewed 82

I have this table and I would like to increase the value in 'column2' by 1. I just want to increase the number, not progress the letter. Also, 'column2' is a copy of 'column1', the only difference being 'column2' has an added value of +1.

Current table:

column1 column2
a1 a1
a2 a2
a3 a3

Desired result:

column1 column2
a1 a2
a2 a3
a3 a4

How do I add +1 and apply that methodology to column2 entirely? It seems really simple but I can't figure it out for the life of me.

Thanks

1 Answers

If it's always one letter and one integer, as you mentioned in comments, then you can do the following (df is your dataframe):

df['column2']=df['column2'].apply(lambda x: x[0]+str(int(x[1])+1))

print(df)

result:

  column1 column2
0      a1      a2
1      a2      a3
2      a3      a4
Related