Reputation:
I'm new to Python, so my question may be very basic, sorry//
I'm struggling to create one dummy variable on two columns in Python. I have a column(died) which is a dummy on its own with 1 corresponding to a death, 0 - no death. The second column is 'Age' that tells the age of death in months. What i need is to create a dummy for children who died before 5 years ('died'==1 & 'Age' < 60) and a dummy for children who died before 1 year ('died' == 1 & Age' < 12). I usually work in Stata in which this is very easy, but in Python I am struggling. I've been trying to use get_dummies function from pandas:
dummy= pd.get_dummies(df['died']) & (df.aad < 60.).astype('int')
but it returns an error that it can't perform add, my guess is that it can't add indicator variable'died' with a continuous variable 'aad'. Is there a straightforward (beginner friendly) way to combine information from two columns to generate a new dummy variable? Thanks a lot!
Upvotes: 0
Views: 480
Reputation: 71
import numpy as np
df['dummy'] = np.where((df['died']==1) & (df['aad']<60), 1, 0)
Upvotes: 1
Reputation: 1862
You could do this pretty easily this way:
dummy = ((df['died'] == 1) & (df['aad'] < 60)).astype('int')
Upvotes: 0