This is my df.
index firmcode year indcode ROA
0 a 2006 03 0.1
1 b 2006 03 0.2
2 c 2006 03 0.4
3 d 2006 03 0.7
4 e 2006 07 0.3
5 f 2006 07 0.8
6 g 2006 07 1.1
7 h 2006 07 2.1
I would like it to be as follows. This is ROA matching the nearest company(within the same year and same indcode, except for the same company)
index firmcode year indcode ROA diff_min_firmcode
0 a 2006 03 0.1 b
1 b 2006 03 0.2 a
2 c 2006 03 0.4 b
3 d 2006 03 0.7 c
4 e 2006 07 0.3 f
5 f 2006 07 0.8 g
6 g 2006 07 1.1 f
7 h 2006 07 2.1 g
How can I get the df['diff_min_firmcode'] column?