Johan Nilssons Lifestream

getting count in pandas python

I have a dataframe like the following:

boss_id    employee_id      designation        
 -1           100              CEO
100           39               Manager 
100          4567              Manager
100          9843              Manager
39            47               entry level
39            45               entry level
4567          8                entry level
9843          9                entry level 

In this boss_id gives the boss of the employee. Designation is for the employee. I want to find how many people each person manages in total.

For instance, since CEO is the ultimate person, he should be managing all 7 people in this dataframe. Managers manage just the entry level. For instance, employee 39 who is a manager manages 2 people in this dataframe. Finally, the entry levels don't manage anyone, so their count should be 0.

I want a dataframe like this:

boss_id    employee_id      designation              count
 -1           100              CEO                     7
100           39               Manager                 2
100          4567              Manager                 1
100          9843              Manager                 1
39            47               entry level             0
39            45               entry level             0
4567          8                entry level             0
9843          9                entry level             0

I can't get my head around this and any help would be much appreciated! Thanks in advance.

via Stack Overflow

blog comments powered by Disqus
Get the source for phplifestream at Github