How to make a column based on previous values in dataframe
Use duplicated:
df['target'] = df.duplicated().astype(int)
print(df)
Output
user_id url target
0 111 google.com 0
1 111 youtube.com 0
2 111 youtube.com 1
3 111 google.com 1
4 111 stackoverflow.com 0
5 111 google.com 1
6 222 twitter.com 0
7 222 google.com 0
8 222 twitter.com 1
df['target'] =df.groupby(['user_id','url']).cumcount().gt(0).astype(int)
user_id url target
0 111 google.com 0
1 111 youtube.com 0
2 111 youtube.com 1
3 111 google.com 1
4 111 stackoverflow.com 0
5 111 google.com 1
6 222 twitter.com 0
7 222 google.com 0
8 222 twitter.com 1