Python Forum
Create new variable dependent on two existing variables - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Create new variable dependent on two existing variables (/thread-30514.html)



Create new variable dependent on two existing variables - JoeOpdenaker - Oct-24-2020

#create trial type to determine stimulus switching vs recurrent
def TrialType(Congruency):
if 'Congruency' = 'pCongruency' return 'True'
elif 'Congruency' != 'pCongruency' return 'False'

...
else:
return '.'
I have two vairalbes: 'Congruency' and 'pCongruency' where each variable has possible values of 1,2, or 3. Using SAS I was able to create a new varialbe 'TrialType' by doing a function similar to the one above. From comments so far it seems I should have return values of true or false.

I am certain there are syntax errors. I am 12 hours into python.


RE: Create new variable dependent on two existing variables - perfringo - Oct-24-2020

(Oct-24-2020, 04:52 AM)JoeOpdenaker Wrote: I need to create a variable "trial_type" that tells me if "congruency" is equal to "pcongruency".

If this problem statement is correct then your function should return boolean value (either True or False).


RE: Create new variable dependent on two existing variables - buran - Oct-24-2020

you say you want a variable, yet you show function. Also where pCongruency comes from into the function, is it global?


RE: Create new variable dependent on two existing variables - ndc85430 - Oct-24-2020

There are even more problems:

1. Syntax errors for checking equality or inequality.
2. You seem to be comparing strings, rather than variables. Presumably that's not what you intended.


RE: Create new variable dependent on two existing variables - JoeOpdenaker - Oct-24-2020

#create trial type to determine stimulus switching vs recurrent
def TrialType(Congruency):
if 'Congruency' = 'pCongruency' return 'True'
elif 'Congruency' != 'pCongruency' return 'False'

...
else:
return '.'



I have two vairalbes: 'Congruency' and 'pCongruency' where each variable has possible values of 1,2, or 3. Using SAS I was able to create a new varialbe 'TrialType' by doing a function similar to the one above. From comments so far it seems I should have return values of true or false.

I am certain there are syntax errors. I am 12 hours into python.


RE: Create new variable dependent on two existing variables - buran - Oct-25-2020

why do you need such function in a first place

spam = 2
eggs = 3
foo = 2

bar = spam == eggs
print(bar)

bar = spam == foo
print(bar)
Output:
False True
Note, these are bool values True and False, not strings "True" and "False"

and of course you can always can do, just

if spam == eggs:
    # do samething



RE: Create new variable dependent on two existing variables - jefsummers - Oct-25-2020

In your if statement, change = to ==