Premium problem122. Rows Where a Product's Price Changed

Medium Locked

prices has columns product_id, date (datetime) and price.

Sorted by date within each product, return the rows where the price differs from that product's previous recorded price. A product's first row counts as a change, since there is nothing before it to match. Keep the original columns, sort by product_id then date, and renumber the index from 0.

The shift has to happen inside each product. A plain shift() over the whole frame compares the last row of one product with the first of the next.

Input

  product_id       date  price
0          a 2022-01-01   10.0
1          a 2022-01-02   10.0
2          a 2022-01-03   12.0
3          a 2022-01-04   12.0
4          b 2022-01-01    5.0
5          b 2022-01-02    5.0

Output

  product_id       date  price
0          a 2022-01-01   10.0
1          a 2022-01-03   12.0
2          b 2022-01-01    5.0

Premium problem

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

Implement solve(...)