Danish
Danish

Reputation: 2871

Create new column in pandas based on multiple specific condition on multiple columns - Pandas

I have a dataframe as shown below

 B_ID  Patient_count    cut_off
1       1               1.000
2       1               1.500
3       1               4.000
4       2               1.200
5       1               3.000
6       1               4.000
7       2               3.600
8       3               0.500
9       4               0.164
12      1               4.500
13      2               2.250
14      1               1.500
15      1               3.500
20      1               3.500
16      1               3.000
17      2               0.400
19      3               0.120

To the above data frame, I would like to create a new column call 'Over_Book' based on below conditions.

if df['Patient_count'] == 1 :
    df['Over_Book'] = 'First'
else if :  df[' cut_off'] <= 1:
    df['Over_Book'] = 'Not_Allowed'
else:
    df['Over_Book'] = 'Allowed'


Expected Output:
B_ID  Patient_count    cut_off     Over_Book
1       1               1.000      First
2       1               1.500      First
3       1               4.000      First
4       2               1.200      Allowed
5       1               3.000      First
6       1               4.000      First
7       2               3.600      Allowed
8       3               0.500      Not_Allowed
9       4               0.164      Not_Allowed
12      1               4.500      First
13      2               2.250      Allowed
14      1               1.500      First
15      1               3.500      First
20      1               3.500      First
16      1               3.000      First
17      2               0.400      Not_Allowed
19      3               0.120      Not_Allowed

Upvotes: 1

Views: 30

Answers (1)

jezrael
jezrael

Reputation: 862441

Use double numpy.where:

df['Over_Book'] = np.where(df['Patient_count'] == 1, 'First',
                  np.where( df['cut_off'] <= 1, 'Not_Allowed', 'Allowed'))

Or numpy.select, but then is necessary for second condition filter out m1 by invert ~m1 and chain by & for bitwise AND:

m1 = df['Patient_count'] == 1
m2 = df['cut_off'] <= 1

df['Over_Book'] = np.select([m1, ~m1 & m2], ['First', 'Not_Allowed'], default='Allowed')

print (df)
    B_ID  Patient_count  cut_off    Over_Book
0      1              1    1.000        First
1      2              1    1.500        First
2      3              1    4.000        First
3      4              2    1.200      Allowed
4      5              1    3.000        First
5      6              1    4.000        First
6      7              2    3.600      Allowed
7      8              3    0.500  Not_Allowed
8      9              4    0.164  Not_Allowed
9     12              1    4.500        First
10    13              2    2.250      Allowed
11    14              1    1.500        First
12    15              1    3.500        First
13    20              1    3.500        First
14    16              1    3.000        First
15    17              2    0.400  Not_Allowed
16    19              3    0.120  Not_Allowed

Upvotes: 2

Related Questions