2

I have a Dataframe with the below column names, and I want to create a new column based on a given condition. The condition are to create a new column Race for each unique EVENT_ID in the dataframe check for rows in the WIN_LOSE that has 1 as value, take the SELECTION_TRAP value that corespond to that WIN_LOSE row and create the Race.

I tried using the pandas where function but I an not getting it right

df['Race'] = df.where(df['WIN_LOSE']==1,df['SELECTION_TRAP'],axis = 0)

EVENT_ID    SELECTION_TRAP  WIN_LOSE
174331755       1             0
174331755       2             0
174331755       7             1
174331755       4             0
174331755       3             0
174331755       6             0
174331755       8             0
174329130       5             0
174329130       7             1
174329130       1             0
174329130       4             0
174329130       2             0
174329130       8             0

My expected output should look this.

EVENT_ID    SELECTION_TRAP  WIN_LOSE  RACE
174331755       7             1        7
174329130       7             1        7
2
  • Whats happen if no 1 per group? Commented Sep 22, 2021 at 9:45
  • Whats happens if multiple 1 per group ? Commented Sep 22, 2021 at 9:45

2 Answers 2

1

First filter by 1 in WIN_LOSE by boolean indexing and then assign new column.

df1 = df[df['WIN_LOSE']==1].copy()
df1['RACE'] = df1['SELECTION_TRAP']
print (df1)
    EVENT_ID  SELECTION_TRAP  WIN_LOSE  RACE
2  174331755               7         1     7
8  174329130               7         1     7
Sign up to request clarification or add additional context in comments.

Comments

1

IIUC, you can create your column called 'RACE', when 'WIN_LOSE' equals 1, and then filter your df:

df.loc[df['WIN_LOSE'].eq(1),'RACE'] = df['SELECTION_TRAP']
df.loc[df.RACE.notnull()]

Prints back:

    EVENT_ID  SELECTION_TRAP  WIN_LOSE  RACE
2  174331755               7         1   7.0
8  174329130               7         1   7.0

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.