Reputation: 850
I have dataframe where I want to keep on increasing the value until val
changes and when id
changes reset the count
value
data = [['p1','1'],
['p1','1'],
['p1','2'],
['p2','3'],
['p2','5'],
['p3','2']]
df = pd.DataFrame(data = data,columns = ['id','val'])
Desired output
id val count
0 p1 1 1
1 p1 1 1
2 p1 2 2
3 p2 3 1
4 p2 5 2
5 p3 2 1
I could only come up with this which does the cumsum but I want a counter
df_1.groupby('id')['val'].cumsum()
My unwanted output
id val count
0 p1 1 1
1 p1 1 2
2 p1 2 4
3 p2 3 3
4 p2 5 8
5 p3 2 2
Upvotes: 1
Views: 51
Reputation: 323316
You can try with transform
+factorize
df['count']=df.groupby('id').val.transform(lambda x : x.factorize()[0]+1)
0 1
1 1
2 2
3 1
4 2
5 1
Name: val, dtype: int32
Upvotes: 1