0

I have a pandas dataframe that looks like this:

+-----+--------+-------+
| Age | PhysID | PedID |
+-----+--------+-------+
|  28 |    111 |   123 |
|  26 |    111 |   123 |
|   3 |    111 |   123 |
+-----+--------+-------+

I would like to create a new column called DocID which has a value equal to PhysID if Age>18 or equal to PedID otherwise. The output would look like this:

+-----+--------+-------+-------+
| Age | PhysID | PedID | DocID |
+-----+--------+-------+-------+
|  28 |    111 |   123 |   111 |
|  26 |    111 |   123 |   111 |
|   3 |    111 |   123 |   123 |
+-----+--------+-------+-------+

Is there a clean way of doing this using some inbuilt functions instead of writing my own? Thanks!

Nexaspx
  • 371
  • 4
  • 20

2 Answers2

3

Using np.where

df['NewId']=np.where(df.Age>18,df.PhysID,df.PedID)
df
   Age  PhysID  PedID  NewId
0   28     111    123    111
1   26     111    123    111
2    3     111    123    123
BENY
  • 317,841
  • 20
  • 164
  • 234
2

lambda functions are good for this sort of problem

df = pd.DataFrame({'Age':[28,26,3],'PhysID':[111,111,111],'PedID':[123,123,123]})

df['DocId'] = df.apply(lambda x: x['PhysID'] if x['Age'] > 18 else x['PedID'], axis=1)

print(df)