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)
1

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. How would I search in multiple columns for the same keywords?

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.