Using a loop and defaultdict
a = defaultdict(lambda:None) longest = defaultdict(int) current = defaultdict(int) for i, j, k in df.itertuples(index=False): if a[(i, j)] == k - 1: current[(i, j)] += 1 if current[(i, j)] else 2 longest[(i, j)] = max(longest[(i, j)], current[(i, j)]) else: current[(i, j)] = 0 longest[(i, j)] |= 0 a[(i, j)] = k pd.concat( [pd.Series(d) for d in [longest, current]], axis=1, keys=['longest_streak', 'last_streak'] ).rename_axis(['user_id', 'product_id']).reset_index() user_id product_id longest_streak last_streak 0 1 1 3 3 1 1 2 0 0 2 2 1 3 3 3 3 1 2 0
source share