2

I have a data frame as follow:

Obs. ID   Name type
  1) 123  abc  duplicate
  2) 123  abc  duplicate
  3) 145  abc  abc
  4) 156  abc  duplicate
  5) 156  abc  duplicate

if ID is same, like in obs. 1 and 2 or 4 and 5 then I want to create a new variable type=duplicate else type=vaule in Name variable(i.e abc)

2 Answers 2

1

We can use duplicated with np.where to set the values according to the result:

df['type'] = np.where(df.duplicated('ID', False), 'Duplicate', 'Single')

print(df)

  Obs.   ID Name       type
0   1)  123  abc  Duplicate
1   2)  123  abc  Duplicate
2   3)  145  abc     Single
3   4)  156  abc  Duplicate
4   5)  156  abc  Duplicate

For the update, you just need a simple tweek:

df['type'] = np.where(~df.duplicated('ID', False), df.Name, 'Duplicate')

print(df)

  Obs.   ID Name       type
0   1)  123  abc  Duplicate
1   2)  123  abc  Duplicate
2   3)  145  abc        abc
3   4)  156  abc  Duplicate
4   5)  156  abc  Duplicate
Sign up to request clarification or add additional context in comments.

3 Comments

could you tell me how to assign value of Name variable in TYPE column when ID is NOT Duplicate(i.e. type=single).
But tha's a different question overall right @karan ?
As a future reference @karan , note that it is not well seen to change questions. It will invalidate some of the answers, and make it look like they're not properly answering the question
0
df['Dup'] = df.ID.duplicated(keep=False).map({True: 'dup', False: 'single'})

df
  Obs.   ID Name     Dup
0   1)  123  abc     dup
1   2)  123  abc     dup
2   3)  145  abc  single
3   4)  156  abc     dup
4   5)  156  abc     dup

1 Comment

could you tell me how to assign value of Name variable in TYPE column when ID is NOT Duplicate(i.e. if type=single then type=abc)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.