0

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!

2
  • why the astype('int') and what is aad Commented May 1, 2020 at 20:50
  • astype('int') is what i found in google on how to convert continuous variables into dummies, "aad" is just a variable name Commented May 3, 2020 at 19:06

2 Answers 2

1

import numpy as np

df['dummy'] = np.where((df['died']==1) & (df['aad']<60), 1, 0)

Sign up to request clarification or add additional context in comments.

Comments

0

You could do this pretty easily this way:

dummy = ((df['died'] == 1) & (df['aad'] < 60)).astype('int')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.