1

I have a column filled with the same string on all rows (E.g 'asd12_01'). Is it possible to increment 1 to all rows? here is what it would look like at the end:

1 'asd12_01'
2 'asd12_02'
3 'asd12_03'

I tried creating a simple function but it did not work. I tried cumcount too, but also didnt work

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • Does this answer your question? [getting the index of a row in a pandas apply function](https://stackoverflow.com/questions/26658240/getting-the-index-of-a-row-in-a-pandas-apply-function) – JonSG Aug 14 '23 at 18:53
  • 1
    Is that a well-known string or do you need to parse the existing string somehow? How would a string that goes over 100 look? – tdelaney Aug 14 '23 at 19:04
  • Can you have strings other than `asd12_xx`? – mozway Aug 14 '23 at 19:14

2 Answers2

3

Using str.extract and groupby.cumcount:

df['new'] = (df['col']
             .str.extract(r'^(.*?)\d*$', expand=False)
             .add(df.groupby('col').cumcount()
                    .add(1).astype(str).str.zfill(2))
             )

Output:

        col       new
0  asd12_01  asd12_01
1  asd12_01  asd12_02
2  asd12_01  asd12_03
mozway
  • 194,879
  • 13
  • 39
  • 75
0
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
Karim Baidar
  • 677
  • 3
  • 10