How to speed up the following python code:
code: df['aft_rest_ind'] = np.abs(df['restart'] - df['restart'].shift(1, fill_value=0))
df['aft_rest_ind'] = df.groupby('id')['aft_rest_ind'].cumsum()
after_day = df[(df['aft_rest_ind']%2==0) & (df['aft_rest_ind']!=0)].groupby('id')['aft_rest_ind'].value_counts()
df['after_restart'] = 0
for i,j in after_day.index:
    if df[(df['id']==i) & (df['aft_rest_ind']==j)].shape[0]>21:
        a = np.append(np.ones(21), (np.zeros(df[(df['id']==i) & (df['aft_rest_ind']==j)].shape[0]-21)))
        df.loc[(df['id']==i) & (df['aft_rest_ind']==j), 'after_restart']=a

    else:
        df.loc[(df['id']==i) & (df['aft_rest_ind']==j), 'after_restart']=1 Write exactly 5 paragraphs each separated with two new lines answering this question. The 3rd paragraph must start with "action".
user