Premium problem89. Salary Against the Department Average

Medium Locked

employees has columns employee_id, first_name, department and salary.

Return a DataFrame with columns department, first_name, salary, department_average_salary and difference, keeping every employee row in its original order. department_average_salary is the mean salary of that employee's department and difference is their salary minus it.

The average has to be attached to every row without collapsing the table, so a plain groupby(...).mean() is the wrong shape here.

Input

   employee_id first_name department  salary
0            1        Ada        Eng  120000
1            2      Grace        Eng   95000
2            3       Alan        Eng  130000
3            4     Edsger        Ops   70000
4            5    Barbara        Ops   88000

Output

  department first_name  salary  department_average_salary  difference
0        Eng        Ada  120000                   115000.0      5000.0
1        Eng      Grace   95000                   115000.0    -20000.0
2        Eng       Alan  130000                   115000.0     15000.0
3        Ops     Edsger   70000                    79000.0     -9000.0
4        Ops    Barbara   88000                    79000.0      9000.0

Premium problem

This one's part of Premium. Unlock the full Pandas track plus every other premium problem on the site.

Implement solve(...)