Jack
Jack

Reputation: 1754

Replace values of duplicated rows with first record in pandas?

Input

 df

id   label
a      1
b      2
a      3
a      4
b      2
b      3
c      1
c      2
d      2
d      3

Expected

  df

id   label
a      1
b      2
a      1
a      1
b      2
b      2
c      1
c      1
d      2
d      2

For id a, the label value is 1 and id b is 2 because 1 and 2 is the first record for a and b.

Try

I refer this post, but still not solve it.

Upvotes: 1

Views: 27

Answers (1)

BENY
BENY

Reputation: 323226

Update with transform first

df['lb2']=df.groupby('id').label.transform('first')
df
Out[87]: 
  id  label  lb2
0  a      1    1
1  b      2    2
2  a      3    1
3  a      4    1
4  b      2    2
5  b      3    2
6  c      1    1
7  c      2    1
8  d      2    2
9  d      3    2

Upvotes: 2

Related Questions