w3resource

Validating Email format in a column using Regex in Pandas


13. Validating the Format of Email Addresses

Write a Pandas program that validates the format of email addresses.

Following exercise validate the format of email addresses in a column using a regular expression.

Sample Solution :

Code :

import pandas as pd
# Create a sample DataFrame with email addresses
df = pd.DataFrame({
    'Email': ['[email protected]', 'invalid-email', '[email protected]']
})

# Validate email format using a regular expression
valid_emails = df['Email'].str.contains(r'^[\w\.-]+@[\w\.-]+\.\w+$')

# Output the result
print(valid_emails)

Output:

0     True
1    False
2     True
Name: Email, dtype: bool

Explanation:

  • Created a DataFrame with email addresses.
  • Used str.contains() with a regex pattern to validate the format of the email addresses.
  • Outputted a Boolean Series indicating whether each email is valid.

For more Practice: Solve these Related Problems:

  • Write a Pandas program to validate the format of email addresses in a column using regular expressions and list invalid emails.
  • Write a Pandas program to extract domains from email addresses and validate that they match a predefined set of allowed domains.
  • Write a Pandas program to check that all email addresses in a column contain a valid top-level domain and flag any that do not.
  • Write a Pandas program to validate email addresses and count the number of entries that fail the standard email format.

Python-Pandas Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.