I have a dataframe
x c
0 0 1
1 3 2
2 1 1
3 2 1
4 3 1
5 4 1
6 1 0
7 3 1
8 2 1
9 1 2
I would like to produce
c x duplicated
0 1 0 False
1 2 3 False
2 1 1 False
3 1 2 True
4 1 3 True
5 1 4 False
6 0 1 False
7 1 3 True
8 1 2 True
9 2 1 False
that is to group by c
first, and mark all duplicated rows in the group.
My current approach is
c = np.random.randint(0, 3, 10)
x = np.random.randint(0, 5, 10)
d = pd.DataFrame({'x': x, 'c': c})
d['duplicated'] = d.groupby('c').apply(
lambda x: x.duplicated(keep=False)
).reset_index(level=0, drop=True)
Is there any better way?