Sample dataset:
import pandas as pd
df = pd.DataFrame({'Year':[1995, 1996, 1997, 1998, 1999, 2000,1995, 1996, 1997, 1998, 1999, 2000],
'Group':['A', 'A', 'A','A', 'A', 'A', 'B', 'B', 'B', 'B', 'B', 'B'],
'Col_1':[100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200],
'Col_2':[11, 22, 33, 44, 55, 66, 77, 88, 99, 199, 299, 399]})
I'd like to create a new column by dividing one year by its previous year in Col_1 and Col_2 respectively for each group. Then, divide the two divisions. See example below:
| Year | Group | Col_1 | Col_2 | New Column |
|---|---|---|---|---|
| 1995 | A | 100 | 11 | NA |
| 1996 | A | 200 | 22 | (200/100)/(22/11) |
| 1997 | A | 300 | 33 | (300/200)/(33/22) |
| 1998 | A | 400 | 44 | ............. |
| 1999 | A | 500 | 55 | ............. |
| 2000 | A | 600 | 66 | ............. |
| 1995 | B | 700 | 77 | NA |
| 1996 | B | 800 | 88 | (800/700)/(88/77) |
| 1997 | B | 900 | 99 | (900/800)/(99/88) |
| 1998 | B | 1000 | 199 | ............. |
| 1999 | B | 1100 | 299 | ............. |
| 2000 | B | 1200 | 399 | ............. |