0

I would like to identify doctors based on their title in a dataframe and create a new column to indicate if they are a doctor but I am struggling with my code.

doctorcriteria = ['Dr', 'dr']

def doctor(x):
  if doctorcriteria in x:
    return 'Doctor'
  else:
    return 'Not a doctor'

df['doctorcall'] = df.caller_name
df.doctorcall.fillna('Not a doctor', inplace=True)
df.doctorcall = df.doctorcall.apply(doctor)
Sanch
  • 367
  • 2
  • 11
  • kindly share a sample of your dataframe and ur expected output: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – sammywemmy Feb 13 '20 at 09:27

1 Answers1

3

To create a new column with a function, you can use apply:

df = pd.DataFrame({'Title':['Dr', 'dr', 'Mr'],
               'Name':['John', 'Jim', 'Jason']})

doctorcriteria = ['Dr', 'dr']

def doctor(x):
    if x.Title in doctorcriteria:
        return 'Doctor'
    else: return 'Not a doctor'

df['IsDoctor'] = df.apply(doctor, axis=1)

But a more direct route to the answer would be to use map on the Title column.

doctor_titles = {'Dr', 'dr'}

df['IsDoctor'] = df['Title'].map(lambda title: title in doctor_titles)
Jamie
  • 146
  • 7