I have three columns,['date'] which has the date, ['id'] which holds product id's and ['rating'] which holds product ratings for each product for each data, I want to create a dummy variable ['threshold'] which equals 1 when within the same value of ['id'] the value of rating went from anywhere above 5 to anywhere below 6. My code would use a for loop as follows:
df['threshold']=np.zeros(df.shape[0])
for i in range(df.shape[0]):
if df.iloc[i]['id'] == df.iloc[i-1]['id'] and df.iloc[i-1]['rating']>5 and df.iloc[i]['rating']<6:
df.iloc[i]['threshold']=1
Is there a way to perform this without using a for loop?