Premium problem158. Change From the Previous Order

Medium Locked

orders has columns order_id, customer_id, placed_at (datetime) and amount.

Add a change column holding the amount minus that customer's previous order amount, in chronological order. A customer's first order has no predecessor and gets NaN rather than reaching back into another customer's history.

Return the original columns plus change, sorted by customer_id then placed_at, with the index renumbered from 0.

Input

   order_id  customer_id  placed_at  amount
0         1            1 2022-01-01    10.0
1         2            1 2022-01-05    25.0
2         3            2 2022-01-02    50.0
3         4            1 2022-01-09    20.0

Output

   order_id  customer_id  placed_at  amount  change
0         1            1 2022-01-01    10.0     NaN
1         2            1 2022-01-05    25.0    15.0
2         4            1 2022-01-09    20.0    -5.0
3         3            2 2022-01-02    50.0     NaN

Premium problem

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

Implement solve(...)