Skip to content Skip to sidebar Skip to footer

Pandas Increment Values On Groupby With A Condition

Lets say I have a df like this, need to groupby on links, and if a link repeated more than 3 times, should increment its value name links A https://a.com/-pg0 B https://b.co

Solution 1:

You can try cumcount with //

s = df.groupby('links').cumcount()//3
Out[125]: 
0    0
1    0
2    0
3    0
4    0
5    1
6    1
7    0
8    0
dtype: int64

df['links'] = df['links'] + s.astype(str)

Post a Comment for "Pandas Increment Values On Groupby With A Condition"