简体   繁体   中英

Dummy variable from two columns in Python

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!

import numpy as np

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

You could do this pretty easily this way:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM