1

I got this dataframe

Contact Title
1 Dear Sir White,
2 Dear Madam Ford,
3 Dear Sir / Madam Hendricx,

I want to make another column called 'Gender' based on the column 'Title'

Contact Title Gender
1 Dear Sir White, M
2 Dear Madam Ford, F
3 Dear Sir / Madam Hendricx, O

So Dear Sir is M(ale)

Dear Madam is F(emale)

Dear Sir / Madam is O

I hope someone can help!

1
  • You want to clearly define how you want to identify gender from these salutations. Merely writing a regex seems less than sufficient for that kind of task. Commented Jul 12, 2021 at 18:14

3 Answers 3

3
df.loc[df['title'].str.contains("Sir"), 'Gender'] = 'Male'
df.loc[df['title'].str.contains("Madam"), 'Gender'] = 'Female'
df.loc[df['title'].str.contains(" Sir / Madam"), 'Gender'] = '0'

also, check this

Sign up to request clarification or add additional context in comments.

Comments

1
g = []

for text in df.Title:
    if ' Sir / Madam' in text:
        g.append('O')
    elif 'Sir' in text:
        g.append('M')
    else:
        g.append('F')

df["Gender"] = g

Comments

0
def my_func(text):
    if 'Sir / Madam' in text:
        return 'O'
    elif 'Madam' in text:
        return 'F'
    else:
        return 'M'

df['Gender'] = df['Title'].apply(my_func)

Comments

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.