I have a Pandas dataframe similar to
d = {'id': [1, 2, 2, 3], 'year': [2010, 2010,2011,2010], 'type' : ['A','B','B','A'], 'value': [20,2,8,3]}
df = pd.DataFrame(data = d)
That is
id year type value
0 1 2010 A 20
1 2 2010 B 2
2 2 2011 B 8
3 3 2010 A 3
I want to add a new column which contains the value one year later for the same id
and type
, if it exists in the df (0 otherwise). That is, the expected result is
id year type value new_value
0 1 2010 A 20 0
1 2 2010 B 2 8
2 2 2011 B 8 0
3 3 2010 A 3 0
I cannot figure out a way of doing that (I have been experimenting mostly with apply). Any suggestions?