I have a table which has various columns but I will only mention those which are on focus of interest.
| Year | Current Amount | Previous Amount | Type |
|---|---|---|---|
| 2021 | 22000 | 36000 | C5 70 |
| 2022 | 26000 | 48000 | C4 68 |
| 2023 | 76440 | 34000 | C5 70 |
| 2024 | 21399 | 12000 | C4 68 |
| 2024 | 22000 | 11000 | C4 34 |
| 2024 | 26500 | 13000 | C4 23 |
| 2025 | 16000 | 34000 | C4 68 |
| 2025 | 76000 | 22000 | C4 34 |
| 2025 | 34000 | 64000 | C4 23 |
Now obviously there are way to many year values. What I want is to deduct the current amount value when year is suppose 2022 from previous amount when year is 2021, In simple words, Subtract Current amount from Previous amount where every year value for Previous amount is preceding than the default value. For instance I want the results to look like this.
| Year | Current Amount | Previous Amount | Type | Change |
|---|---|---|---|---|
| 2021 | 22000 | 36000 | C5 70 | Nan |
| 2022 | 26000 | 48000 | C4 68 | -10,000 |
| 2023 | 76440 | 34000 | C5 70 | 28,440 |
| 2024 | 21399 | 12000 | C4 68 | 35899 |
| 2024 | 22000 | 11000 | C4 34 | 35899 |
| 2024 | 26500 | 13000 | C4 23 | 35899 |
| 2025 | 16000 | 34000 | C4 68 | 90000 |
| 2025 | 76000 | 22000 | C4 34 | 90000 |
| 2025 | 34000 | 64000 | C4 23 | 90000 |
I have tried using following code but no luck so far :
Df['Current Amount'] - Df['Previous Amount'].where(Df['Year']==Df['Year']-1)
Also:
Df['Current Amount'] - Df['Previous Amount'].where(Df['Year']==Df['Year'].shift(-1))
Please help me. Thanks in Advance.