I have a dataframe where I would like to increment strings based on their counts in Python
Data
id  type    date       count
aa  hi      q1 2023    2
aa  hey     q2 2023    3
bb  hi      q1 2023    2
            
Desired
id  count   type    date
aa  hi01    hi      q1 2023
aa  hi02    hi      q1 2023
aa  hey01   hey     q1 2023
aa  hey02   hey     q1 2023
aa  hey03   hey     q1 2023
bb  hi01    hi      q1 2023
bb  hi02    hi      q1 2023
Doing
I believe I have to perform a 'melt' to expand the dataset
(df.melt(id_vars=['id', 'type', 'date'], value_name='count') # reshape data
   .sort_values(by=['date', 'variable'])
A SO user suggested this which works to increment
count=lambda d: d['type']+d.groupby(['id', 'date', 'type']).cumcount().add(1).astype(str).str.zfill(2)
I am researching how to combine these, any suggestion is appreciated
 
    