Several stores each report one sales figure per day. For every row you want to see the figure the same store reported the time before, and how much it moved since then.
Write add_previous_day(df).
df has the columns store (text), date (text in the form YYYY-MM-DD) and sales (a positive number). A store has at most one row per date, and the rows are in no particular order.
Return a new DataFrame sorted by store (A to Z) and, inside a store, by date (earliest first). It has the three original columns followed by:
prev_sales: the sales of the same store's previous row in that orderchange: sales - prev_salespct_change: 100 * (sales - prev_sales) / prev_salesThe first row of every store has no previous row. In that row all three new columns are missing (NaN).
YYYY-MM-DD sort correctly as text, so you can sort them without converting them.df. The index of the result does not matter.groupby(...).apply(...) for this task. Recent pandas versions warn when the applied function returns the grouping column, and newer versions leave that column out of the result.