Общее решение:
print (df)
ID year Value
1 3 2003 95
2 3 1995 200
2 3 2001 100
3 4 1995 300
4 4 2000 250
5 4 2004 150
6 5 2000 201
7 5 1995 202 <- remove this row with 1995, because last value of group 5, if seelct next row it is in another group
8 6 2000 203
9 6 2000 204
Сначала выберите индексы 1995
и все следующие строки:
idx = df.index[(df['year'] == 1995) & (df.groupby('ID').cumcount(ascending=False) != 0)]
idx2 = df.index.intersection(idx + 1).union(idx)
df = df.loc[idx2]
print (df)
ID year Value ValuedFrom
2 3 1995 200 1995
2 3 2001 100 2001
3 4 1995 300 1995
4 4 2000 250 2000
Деталь
print (df.groupby('ID').cumcount(ascending=False))
1 2
2 1
2 0
3 2
4 1
5 0
6 1
7 0
8 1
9 0
dtype: int64
Затем измените значения столбца year
для изменения на unstack
:
df['year'] = np.where(df['year'] == 1995, 'Value1995', 'ValueRetired')
df = df.set_index(['ID', 'year'])['Value'].unstack().reset_index().rename_axis(None, axis=1)
print (df)
ID Value1995 ValueRetired
0 3 200 100
1 4 300 250