I have a data frame as shown below
df=pd.DataFrame({'item':['a','a','a','a','a','b','b','b','b'],
'date':['2020-03-14','2020-03-21','2020-03-28',
'2020-04-04','2020-04-11','2020-08-08','2020-08-15',
'2020-08-22','2020-08-29'],
'value':[1,2,3,4,6,2,4,8,16]})
I want to create new data frame df_slope as follows
df_output=pd.DataFrame({'item':['a','b'],
'slope':[1.5,6.0]})
here slope is calculated as round(np.polyfit([1,2,3],[y1,y2,y3], 1)[0],2) where y1,y2,y3 is the latest three data points.
How do I create this data frame? Thanks in advance.
Update
How do I handle insufficient data here? i.e if we don't have 3 data points, say just 2 data points I still want to compute the slope.