Adding prefixes like “Mr.” or “Mrs.” to names in Excel can be a useful task, especially when dealing with large datasets. In this guide, we’ll show you how to use the Python library gender-guesser to automatically add prefixes to names based on gender. This approach can save time and effort compared to manually adding prefixes, especially when working with a large number of names.
Example:
Let’s say you have an Excel file (names.xlsx) with a column of names and you want to add a prefix column with “Mr.” for male names and “Mrs.” for female names. Here’s how you can do it using Python and the gender-guesser library:
- Install the
gender-guesserlibrary if you haven’t already:
pip install gender-guesser
Created Excel with names:

Now let us go for the code to add a column known as prefix in the same sheet as above which is names.xlsx which has Mr & Mrs written against names after identifying whether it is male or female.
Code:
import pandas as pd
import gender_guesser.detector as gender
from openpyxl import load_workbook
# Load the Excel file
df = pd.read_excel('names.xlsx')
# Create an instance of the gender detector
d = gender.Detector()
# Function to add prefix based on gender
def add_prefix(row):
name = row['Name']
gender = d.get_gender(name.split()[0])
if gender in ['male', 'mostly_male']:
return 'Mr. ' + name
elif gender in ['female', 'mostly_female']:
return 'Mrs. ' + name
# Apply the function to the 'Name' column and create a new column 'Prefix'
df['Prefix'] = df.apply(add_prefix, axis=1)
# Load the Excel file into openpyxl
wb = load_workbook('names.xlsx')
ws = wb.active
# Iterate over the rows and update the 'Prefix' column with the prefixed names
for idx, value in enumerate(df['Prefix'], start=2): # Start from row 2 to skip header
ws.cell(row=idx, column=2, value=value) # Assuming 'Prefix' column is the second column
# Save the modified Excel file
wb.save('names.xlsx')
Explanation of the above code:
import pandas as pd: Imports the pandas library and assigns it the aliaspd, which is a commonly used convention.import gender_guesser.detector as gender: Imports theDetectorclass from thegender_guesser.detectormodule and assigns it the aliasgender.from openpyxl import load_workbook: Imports theload_workbookfunction from theopenpyxllibrary, which is used to load an existing Excel file.df = pd.read_excel('names.xlsx'): Reads the Excel file'names.xlsx'into a pandas DataFrame nameddf.d = gender.Detector(): Creates an instance of theDetectorclass from thegender_guesserlibrary, which is used to guess the gender of names.def add_prefix(row):: Defines a functionadd_prefixthat takes a row of data from the DataFrame as input.name = row['Name']: Retrieves the value in the'Name'column from the input row.gender = d.get_gender(name.split()[0]): Uses theDetectorinstancedto guess the gender of the first word in the name.if gender in ['male', 'mostly_male']:: Checks if the guessed gender is male or mostly male.return 'Mr. ' + name: If the gender is male or mostly male, returns the name prefixed with'Mr. '.elif gender in ['female', 'mostly_female']:: Checks if the guessed gender is female or mostly female.return 'Mrs. ' + name: If the gender is female or mostly female, returns the name prefixed with'Mrs. '.df['Prefix'] = df.apply(add_prefix, axis=1): Applies theadd_prefixfunction to each row of the DataFramedfalong axis 1 (column-wise) and creates a new column'Prefix'with the prefixed names.wb = load_workbook('names.xlsx'): Loads the Excel file'names.xlsx'into an openpyxlWorkbookobject namedwb.ws = wb.active: Gets the active worksheet from theWorkbookobjectwband assigns it to the variablews.for idx, value in enumerate(df['Prefix'], start=2):: Iterates over the prefixed names in the'Prefix'column of the DataFramedf, starting from row 2 (to skip the header).ws.cell(row=idx, column=2, value=value): Updates the cell in the'Prefix'column (column=2) of the worksheetwsat the current rowidxwith the valuevalue(the prefixed name).wb.save('names.xlsx'): Saves the modified Excel file back to'names.xlsx'.
This code reads an Excel file containing names, guesses the gender of each name, adds a prefix (‘Mr.’ or ‘Mrs.’) based on the gender, and then saves the modified names back to the same Excel file with an additional ‘Prefix’ column.
Output:
Conclusion:
In this guide, we’ve demonstrated how to use the Python library gender-guesser to automatically add prefixes like “Mr.” or “Mrs.” to names in an Excel file. By leveraging the gender-guesser library, we were able to guess the gender of each name based on the first name and add the appropriate prefix accordingly. This approach can be particularly useful when working with large datasets, as it saves time and ensures consistency in adding prefixes to names.
What else you can do with the gender-guesser library:
- Gender Analysis: The
gender-guesserlibrary can be used to analyze the gender distribution in a dataset based on names. This can be helpful in demographic studies or marketing research. - Data Cleaning: You can use the
gender-guesserlibrary to clean up datasets by standardizing gender-related fields, such as adding prefixes or correcting gender-specific errors. - Personalization: In applications where personalization is important, such as email marketing or customer communication, you can use the
gender-guesserlibrary to address individuals with the appropriate prefix based on their name. - Educational Purposes: The
gender-guesserlibrary can be used in educational settings to teach students about data analysis and gender prediction algorithms.
Overall, the gender-guesser library provides a simple yet effective way to guess the gender of names and perform gender-related analysis and data manipulation tasks in Python.





Leave a Reply