add a "flag" column about whether one ID has certain values in one other column

Viewed 237

The dataframe is like:

 In [1]: df
 Out[2]:
          userid type
    0      1       1
    1      1       2
    2      2       1
    3      3       1
    4      3       2
    5      3       3    

Now I want to add a column to it about whether the userid has certain values in "type" columns(let's say type1 and type2). This is what I desire in output:

 In [1]: df
 Out[2]:
          userid type  has_type_12
    0      1       1      1
    1      1       2      1
    2      2       1      0
    3      3       1      1
    4      3       2      1
    5      3       3      1

Is there a quick solution for this?


I have left out one situation, where userID 3 may has more types like 3 or 4.In this case, I would like to tag has_type_12 = 1 for 3. I have modified the input and the desired output above.

4 Answers
Related