I have such dataframe:
| product1 | product2 | product3 | product4 | product5 | time |
|---|---|---|---|---|---|
| straws | orange | melon | chair | bread | 1 |
| melon | milk | book | coffee | cake | 2 |
| bread | bananas | juice | chair | book | 3 |
| straws | coffee | cake | milk | orange | 4 |
I need to have the time step per items.
Example :
| TimesProduct1 | TimesProduct2 | TimesProduct3 | Timesproduct4 | Timesproduct5 |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 0 | 0 |
| 2 | 0 | 0 | 2 | 1 |
| 3 | 2 | 2 | 2 | 3 |
df.diff() unfortunatly doesn't work in that way.
Thanks for your help.
setup
solution
solution 2 (for pandas bug workaround)