For below sample data:
data={'Person':['a','a','a','a','a','b','b','b','b','b','b'],
'Sales':['50','60','90','30','33','100','600','80','90','400','550'],
'Price':['10','12','8','10','12','10','13','16','14','12','10']}
data=pd.DataFrame(data)
For each person (group) I would like the price based on the second highest Sales on a rolling basis but the window will be different for each group. The result should look like:
result={'Person':['a','a','a','a','a','b','b','b','b','b','b'],
'Sales':['50','60','90','30','33','100','600','80','90','400','550'],
'Price':['10','12','8','10','12','10','13','16','14','12','10'],
'Second_Highest_Price':['','10','12','12','12','','10','10','10','12','10']}
I tried using nlargest(2) but not sure how to get it to work on a rolling basis.