1

I read this which is talking about pd.factorize to identify and create the unique value for the user identify.

However, in my case, I would like to apply the multi condition which is OR condition to identify the user and the condition have the importance ordering.

For example: df:

cond_1(email)  cond_2(phone)  cond_3(other)
abc@yahoo.com  12345678       qwe
asd@yahoo.com  789456123      rty
abc@yahoo.com  905132312      zxc
dsds@yahoo.com 789456123       po
abc@yahoo.com  789456123      special

The expected:

cond_1(email)  cond_2(phone)  cond_3(other) unique_id
abc@yahoo.com  12345678       qwe            1
asd@yahoo.com  789456123      rty            2  
abc@yahoo.com  905132312      zxc            1
dsds@yahoo.com 789456123       po            2
abc@yahoo.com  789456123      special        1
Georgy
  • 12,464
  • 7
  • 65
  • 73
Hong
  • 365
  • 4
  • 14

1 Answers1

1

IIUC, you can do:

df['unique_id']=df.apply(lambda x: pd.factorize(x)[0]+1).min(axis=1)
print(df)

    cond_1(email)  cond_2(phone) cond_3(other)  unique_id
0   abc@yahoo.com       12345678           qwe          1
1   asd@yahoo.com      789456123           rty          2
2   abc@yahoo.com      905132312           zxc          1
3  dsds@yahoo.com      789456123            po          2
4   abc@yahoo.com      789456123       special          1
anky
  • 74,114
  • 11
  • 41
  • 70