在下面的示例中,最好的分组方式是什么,这样就可以有一个新的列,该列是通过在每个组中取第一年并减go 当前年份而形成的.例如,在索引为0的行中,它将是NaN,索引为1的行,它将=1,索引为2的行,它将=3,索引为4的行,等等.
>>> import pandas as pd
>>> df = pd.DataFrame({'id': ['1', '1', '1', '2', '2', '3', '4', '4'],
... 'Year': [2000, 2001, 2003, 2004, 2005, 2002, 2001, 2003]})
>>> print(df)
id Year
0 1 2000
1 1 2001
2 1 2003
3 2 2004
4 2 2005
5 3 2002
6 4 2001
7 4 2003