I have a dataframe of the following format
YR WEEK ACCOUNT Tax AMOUNT SURCHARGE Label
2021 1 1 10 5000 5 0
2021 1 2 20 10000 10 1
2021 2 1 20 2000 2 0
2021 2 2 5 10 3 1
2021 3 1 20 10000 10 1
2021 3 2 10 40000 50 1
I want to reshape it such that, it results in the following
ACCOUNT YR WEEK Tax_1 Amount_1 Surcharge_1 Tax_2 Amount_2 Surcharge_2 Label
1 2021 2 10 5000 5 20 2000 2 0
2 2021 2 20 10000 10 5 10 3 1
1 2021 3 0 2000 2 20 10000 10 1
2 2021 3 5 10 3 10 40000 50 1
Each row is reshaped such that it is a function of the past 2 rows by account, year and week. And the Label is the current label of the WEEK
I tried first to reshape it to wide
df.pivot('ACCOUNT', columns=['YR','WEEK'])
And was going to try a reshape after, but the pivotresulted in NaN values
Any ideas on how I can achieve this in pandas
Thx!