Это должно решить вашу проблему -
def f(col):
#First step is to get the last 2 for each group using .tail(2)
dff = df[['unit','time',col]].sort_values(by=['unit','time'],axis=0).groupby(['unit']).tail(2)
#Next we need the ordered rank of the time values instead of the actual values of time,
#since then we can keep the time values 2,3 as 1,2 and 1,2 as 1,2.
dff['time'] = dff.groupby(['unit']).rank()
#Last we pivot over the time and units to get the columns that you need for correlation analysis
dff = dff.pivot(index='time',columns='unit',values=col).reset_index(drop=True).add_prefix('unit_')
return dff
f('s1')
unit unit_1 unit_2
0 4 5
1 9 3
Используйте эту функцию для более быстрого выполнения.
def f(col):
filt = df[['unit',col]].groupby('unit').tail(2) #filter last 2
filt['count'] = filt.groupby('unit').cumcount() #add a counter column for pivot
#Use counter column as index and unit as column for pivot, then add prefix
filt = filt.pivot(index='count',columns='unit',values=col).reset_index(drop=True).add_prefix("unit_")
return filt