I have a sales data which is in a week-wise format as shown below:
cw | customers |
---|---|
2022_6 | 2190 |
2022_7 | 2148 |
2022_8 | 2407 |
2022_9 | 3280 |
2022_10 | 3915 |
2022_11 | 1396 |
But I want to transpose my data in the format as below: (I am trying to get through with a for loop)
calendar_week | t-1 | t-2 | t-3 | t-4 | t-5 | t-6 |
---|---|---|---|---|---|---|
2022_6 | 2190 | |||||
2022_7 | 2148 | 2190 | ||||
2022_8 | 2407 | 2148 | 2190 | |||
2022_9 | 3280 | 2407 | 2148 | 2190 | ||
2022_10 | 3915 | 3280 | 2407 | 2148 | 2190 | |
2022_11 | 1396 | 3915 | 3280 | 2407 | 2148 | 2190 |
It would be great if anyone could help me with this!
Based on the result dataset t-1
, t-2
etc... columns are more like the shifts of the customers
column of the original dataset shift period starting from 0. So you can generate the DataFrame like this:
for i in range(6):
df[f't-{i+1}'] = df['customers'].shift(i)