FunctionRacerProblemsRace

Three Month Totals

HardpandasPython

pay has columns id, month (1 to 12) and salary, at most one row per person per month. Some months are missing.

Leave out each person's latest month. For every row that is left, total is that person's salary in that month plus their salary in the two months before it (a month with no row adds 0). Return a DataFrame with columns id, month and total, sorted by id, then by month from latest to earliest.

Example 1

pay {"id":[1,1,1,1,2,2,3],"month":[1,2,3,4,1,2,2],"salary":[20,30,40,60,20,30,40]}
returns {"columns":["id","month","total"],"rows":[[1,3,90],[1,2,50],[1,1,20],[2,1,20]]}

Person 1's latest month, 4, is left out. Month 3 totals 40 + 30 + 20, month 2 totals 30 + 20, month 1 is 20. Person 2 keeps only month 1. Person 3 has one month, which is their latest.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More pandas problems