w3resource

Validating Email format in a column using Regex in Pandas

Pandas: Data Validation Exercise-13 with Solution

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.

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/pandas/pandas-validate-email-format-in-a-column-using-regex.php