Manesh Halai
Manesh Halai

Reputation: 281

Conditional column using threshold values

I have a table set up as follows:

Name    ScoreA    ScoreB    
Jimmy   0.95       0.95       
Claire  0.95       0.75      

What I would like to do is is have an output column that returns 1 if both scores are above 0.9, returns 0 if both scores are below 0.8 and returns 2 if both scores are within 0.8 < score < 0.9 OR if one score is above 0.9 and the other is below 0.9.

The output would look something like this:

Name    ScoreA    ScoreB   0/1/2  
Jimmy    0.95      0.95      1
Claire   0.65      0.75      0
Harry    0.85      0.82      2
Paige    0.91      0.82      2

Anyone have any ideas?

Thanks!

Upvotes: 1

Views: 32

Answers (1)

Stef
Stef

Reputation: 30579

Using numpy select:

df['0/1/2'] = np.select([(df.ScoreA<.8)&(df.ScoreB<.8), (df.ScoreA>.9)&(df.ScoreB>.9)], [0,1], 2)

Result:

     Name  ScoreA  ScoreB  0/1/2
0   Jimmy    0.95    0.95      1
1  Claire    0.65    0.75      0
2   Harry    0.85    0.82      2
3   Paige    0.91    0.82      2

Upvotes: 1

Related Questions