Reputation: 1852
So I want to create a new column in my dataframe, let's call it "outcome". I want each value in "outcome" to be determined based off values in two other columns. So let's say this is my dataframe:
raw_data = {
'subject_id': ['1', '2', '3', '4', '5'],
'first_name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'],
'last_name': ['Anderson', 'Ackerman', 'Ali', 'Aoni', 'Atiches']}
df_a = pd.DataFrame(raw_data, columns = ['subject_id', 'first_name', 'last_name'])
This is what out dataframe looks like:
subject_id first_name last_name
0 1 Alex Anderson
1 2 Amy Ackerman
2 3 Allen Ali
3 4 Alice Aoni
4 5 Ayoung Atiches
So I want to create a new column that kind of takes in logic like this:
if(df_a[df_a['subject_id'] == 2] & df_a[df_a['first_name'] == 'Amy']):
df_a['outcome'] = 2
elif(df_a[df_a['subject_id'] > 0] & df_a[df_a['first_name'] == 'Alice']):
df_a['outcome'] = 1
else:
df_a['outcome'] = 0
I cannot seem to get it to work properly though. I expect the new column to look like this:
subject_id first_name last_name outcome
0 1 Alex Anderson 0
1 2 Amy Ackerman 2
2 3 Allen Ali 0
3 4 Alice Aoni 1
4 5 Ayoung Atiches 0
Upvotes: 1
Views: 62
Reputation: 1231
df_a.loc[(df_a['subject_id'] == 2) & (df_a['first_name'] == 'Amy'),'outcome']=2
df_a.loc[(df_a['subject_id'] > 0 ) & (df_a['first_name'] == 'Alice'),'outcome']=1
df_a['outcome'].fillna(0)
Not as good as the above, but looking at what you were trying to do, this will be relevant for you.
Upvotes: 0
Reputation: 862521
Use numpy.select
or numpy.where
:
#first convert `subject_id` to int
df_a['subject_id'] = df_a['subject_id'].astype(int)
m1 = (df_a['subject_id'] == 2) & (df_a['first_name'] == 'Amy')
m2 = (df_a['subject_id'] > 0) & (df_a['first_name'] == 'Alice')
df_a['outcome'] = np.select([m1, m2], [2,1], default=0)
print (df_a)
subject_id first_name last_name outcome
0 1 Alex Anderson 0
1 2 Amy Ackerman 2
2 3 Allen Ali 0
3 4 Alice Aoni 1
4 5 Ayoung Atiches 0
Or:
df_a['outcome'] = np.where(m1, 2, np.where(m2, 1, 0))
print (df_a)
subject_id first_name last_name outcome
0 1 Alex Anderson 0
1 2 Amy Ackerman 2
2 3 Allen Ali 0
3 4 Alice Aoni 1
4 5 Ayoung Atiches 0
Upvotes: 2