I have two columns Letter & Values, new column should have incremented value based on "Letter" column as shown below:
import pandas as pd
df = pd.DataFrame(data=[['a', 'one'],
['a', 'two'],
['b', 'three'],
['b', 'four'],
['c', 'five'],
['c', 'five'],
['c', 'five']
],
columns=['Letter', 'value'])
#df['counter'] = df['value'].shift().where(df['Letter'].shift() == df['Letter'], '')
print(df)
df['counter'] = df.apply(lambda x: x+1 if df['Letter'].shift() == df['Letter'] else 1, axis=1)
#print(df)
'''
Expected output
Letter value counter
0 a one p1
1 a two p2
2 b three p1
3 b four p2
4 c five p1
5 c five p2
6 c five p3
'''
Can you please help me in fixing the above code? Thanks.
Editing my question to be more precise, I need to convert rows to columns later(i have that code working using pivot function)