'Pandas : Get count of repeating column and assign count value in new column [duplicate]

Below is my Data.

CompanyEvents   StartDate:year
Event1              2013
Event1              2013
Event1              2011
Event1              2011
Event1              2012
Event2              2015
Event2              2016
Event2              2016
Event2              2017

I want result is like table below. It should give count of repeating years of same event.

CompanyEvents   StartDate:year  StartDate:year_count
Event1          2013                 2
Event1          2013                 2
Event1          2011                 2
Event1          2011                 2
Event1          2012                 1
Event2          2015                 1
Event2          2013                 1
Event2          2016                 2
Event2          2017                 1
Event3          2011                 1


Solution 1:[1]

There you go, using groupby and transform :

Pandas GroupBy Transform

df['StartDate:year_count'] = df.groupby('StartDate:year')['StartDate:year'].transform('count')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ibrahim Ayoup