| id | order_id | status | customer_id | date |
|---|---|---|---|---|
| 101 | 235 | X | 6788 | 23-07-2021 |
| 102 | 235 | Y | 6788 | 23-07-2021 |
| 103 | 238 | X | 6788 | 23-07-2021 |
| 104 | 235 | X | 6788 | 23-07-2021 |
For each customer_id I want to find their last 4 order_ids.
df[df.customer_id == 6788][df.customer_id.isin(df[df.customer_id == 6788].sort_values('date', ascending=False).order_id.unique()[0:4])]
The above code line works for a single id I am not unsure as to how to do the same for all the customer ids.
The error was
TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed
df[df.groupby('customer_id')][df.customer_id.isin(df[df.groupby('customer_id'))].sort_values('date', ascending=False).order_id.unique()[0:4])]