Premium problem142. Fill Missing Salary With the Department Median

Medium Locked

employees has columns employee_id, department and salary, where some salaries are missing.

Replace each missing salary with the median salary of that employee's department, computed from the rows that do have one. A department where nobody has a salary stays missing. Return the original columns with the index unchanged.

Input

   employee_id department  salary
0            1        Eng   100.0
1            2        Eng     NaN
2            3        Eng   140.0
3            4        Ops     NaN
4            5        Ops     NaN

Output

   employee_id department  salary
0            1        Eng   100.0
1            2        Eng   120.0
2            3        Eng   140.0
3            4        Ops     NaN
4            5        Ops     NaN

Premium problem

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

Implement solve(...)