Python: Conditionals for new variable with three classes

diesmiling

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

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I use multiple conditionals and match to create a new variable?

From Dev

Variable naming in Python classes

From Dev

Python conditionals replacement with polymorphism

From Dev

Conditionals decoration of functions in Python

From Dev

python - while loop with conditionals

From Dev

Python xpath and conditionals

From Dev

Conditionals decoration of functions in Python

From Dev

conditionals with dicts Python

From Dev

Changing Element Classes with jQuery using conditionals and a timer

From Dev

OOP conditionals for greater specificity - Python

From Dev

OOP conditionals for greater specificity - Python

From Dev

Implementing if-else conditionals with Python

From Dev

How can I declare this variable if it can be one of three different classes?

From Dev

Python classes - each class call new id

From Dev

object has no attributes. New to classes in python

From Dev

Create new variable based on comparison between three other variables

From Dev

Python assign text to new variable

From Dev

Python assign text to new variable

From Dev

How to use variable in different classes in Python?

From Java

Is there a way to do conditionals inside Python (3) for loops?

From Dev

Inserting ',' based on conditionals within a STR - Python

From Dev

Conditionals aren't working in my Python code

From Dev

Conditionals for evaluating True and False in a python list comprehension?

From Dev

Python: How to put multiple and/or conditionals in an if statement?

From Dev

Python: Shorten concatenation by using inline conditionals

From Java

Group by + New Column + Grab value former row based on conditionals

From Dev

How to create new vector by using conditionals on another vector?

From Dev

How to create a new variable that contains three calulations that are based on other selected variable' values

From Dev

Three classes with common methods

Related Related

  1. 1

    How can I use multiple conditionals and match to create a new variable?

  2. 2

    Variable naming in Python classes

  3. 3

    Python conditionals replacement with polymorphism

  4. 4

    Conditionals decoration of functions in Python

  5. 5

    python - while loop with conditionals

  6. 6

    Python xpath and conditionals

  7. 7

    Conditionals decoration of functions in Python

  8. 8

    conditionals with dicts Python

  9. 9

    Changing Element Classes with jQuery using conditionals and a timer

  10. 10

    OOP conditionals for greater specificity - Python

  11. 11

    OOP conditionals for greater specificity - Python

  12. 12

    Implementing if-else conditionals with Python

  13. 13

    How can I declare this variable if it can be one of three different classes?

  14. 14

    Python classes - each class call new id

  15. 15

    object has no attributes. New to classes in python

  16. 16

    Create new variable based on comparison between three other variables

  17. 17

    Python assign text to new variable

  18. 18

    Python assign text to new variable

  19. 19

    How to use variable in different classes in Python?

  20. 20

    Is there a way to do conditionals inside Python (3) for loops?

  21. 21

    Inserting ',' based on conditionals within a STR - Python

  22. 22

    Conditionals aren't working in my Python code

  23. 23

    Conditionals for evaluating True and False in a python list comprehension?

  24. 24

    Python: How to put multiple and/or conditionals in an if statement?

  25. 25

    Python: Shorten concatenation by using inline conditionals

  26. 26

    Group by + New Column + Grab value former row based on conditionals

  27. 27

    How to create new vector by using conditionals on another vector?

  28. 28

    How to create a new variable that contains three calulations that are based on other selected variable' values

  29. 29

    Three classes with common methods

HotTag

Archive