1

I want to create a new variable with include all drugs DS59 - DS71 (values currently coded: 1 = never used, 2 = rarely use, 3 = occasionally use, and 4 = regularly use). I want one of three classes to be assigned to each subject as laid out below:

  1. no user: no use on any of the drugs (all 1's)
  2. experimenter/light user: low overall score on drug use across all classes (total summed score less than 20) and no "regularly use (4)" answers to any drug classes
  3. regular user - high overall score on drug use across all classes (score above 20) and at least one "occasionally use (3)" or "regularly use (4)" answer to any drug class

This is my current code - I am unsure how to most appropriately write the conditionals.

druglist=[(df['DS59']),(df['DS60']),(df['DS61']),(df['DS62']),(df['DS63']),
         (df['DS64']),(df['DS65']),(df['DS66']),(df['DS67']),(df['DS68']),
         (df['DS69']),(df['DS70']),(df['DS71'])]

conditions=[
    (druglist== ),
    (druglist==),
    (druglist== ),
]

values=['no user','experimenter/light user','regular user']

df['drugs']=np.select(conditions,values) 

Thank you so much for any help/advice.

Rolv Apneseth
  • 2,078
  • 2
  • 7
  • 19
diesmiling
  • 27
  • 8
  • Don't you need the scores from the subjects? – mhawke Jan 27 '21 at 01:44
  • Yes, I have the scores to each current drug variables (DS59-DS71) for each participant. I am unsure how to take those scores and create a new variable with the three classes described above. For example, how to write the conditional for at least one "occasionally use (3)" or "regularly use (4)" answer to any drug class? – diesmiling Jan 27 '21 at 02:00
  • So does `df['DS59']` return an integer between 1-4 then? – Rolv Apneseth Jan 27 '21 at 12:16
  • @RolvApneseth yes! – diesmiling Jan 27 '21 at 13:53

1 Answers1

0

If I understood correctly, this should be what you're looking for. Let me know if not:

drug_sum = sum(druglist)

conditions = [
    (drug_sum == len(druglist)),  # If it equals the length, that means every item is 1
    (drug_sum <= 20 and not 4 in druglist),
    (drug_sum > 20 and (3 in druglist or 4 in druglist)),
]

Though I'm not sure, do these conditions not leave some cases not fitting into any of the options? For example if a person is 1 on everything but one drug, on which they are 4.

Rolv Apneseth
  • 2,078
  • 2
  • 7
  • 19
  • Thank you. I am getting a value error at line 9 ((drug_sum <= 20 and not 4 in druglist)) (and probably again at line 10). ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). – diesmiling Jan 27 '21 at 15:43
  • Looking at this answer [here](https://stackoverflow.com/a/36922103/14316282) maybe you have to switch them to bitwise operations, so `&` instead of `and` and `|` instead of `or`. Does it work if you do that? – Rolv Apneseth Jan 27 '21 at 15:54
  • That fixed that error. Now however it is saying that "not" is invalid syntax. Thank you again for your continued effort and support. I am extremely grateful. – diesmiling Jan 27 '21 at 20:31
  • Weird, maybe `& !(4 in druglist)`? Sorry I don't use the module you're using often I haven't had problems with conditional statements like this before – Rolv Apneseth Jan 27 '21 at 23:04
  • Oh, no it may just be that it's supposed to be `4 not in druglist` – Rolv Apneseth Jan 27 '21 at 23:05
  • I'll update the answer once (maybe if) we get it working for you – Rolv Apneseth Jan 27 '21 at 23:06
  • Thank you so much(: – diesmiling Jan 27 '21 at 23:32
  • So what changes worked so I can update the answer? The bitwise operations and changing to `not in`? – Rolv Apneseth Jan 28 '21 at 10:57
  • 1
    I ended up going a slightly different route. It is not nearly as elegant and short as the proposed conditional, but it worked. Thank you for your help. I am still not sure what fixes would cause the proposed code to work without error – diesmiling Jan 30 '21 at 00:51
  • Fair enough, if you got it to work that's the most important thing – Rolv Apneseth Jan 30 '21 at 12:26