Data Validation in Python Using Pandas

Introduction to Data Validation

Welcome to data validation! In this lesson, we will delve into data validation using the Pandas library in Python. Data validation ensures the quality and integrity of data, which is crucial for accurate analysis and modeling. We will focus on identifying and handling common data issues such as missing values, duplicate entries, data types, and outliers.

Importance of Data Validation

Data validation is a foundational step in data preparation. It aids in identifying inaccuracies, inconsistencies, and anomalies to prevent erroneous results. Valid data enables analysts to generate reliable insights, support business decisions, and develop robust models. Validation is particularly critical in fields like finance, healthcare, and machine learning, where data quality can significantly impact outcomes. Ensuring data is free from errors minimizes the risk of faulty conclusions and enhances the predictive power of models.

Setting Up Your DataFrame

Before we can perform data validation checks, we need to define a Pandas DataFrame to work with. This DataFrame will serve as the sample dataset for demonstrating the validation techniques, including handling missing values, duplicates, and outliers.

Python
import pandas as pd

# Creating a sample DataFrame
data = {
    'Name': ['John', 'Anna', 'Peter', 'Linda', None, 'Peter'],
    'Age': [28, 24, 35, 32, 87, 35], 
    'Email': ['john@example.com', 'anna@example.com', 'peter@example.com', 'linda@example.com', 'linda@example.com', 'peter@example.com'],
    'Salary': [50000, 52000, 58000, 59000, 1000, 58000] 
}

df = pd.DataFrame(data)

In this example, our DataFrame df contains missing values in the 'Name' column, duplicate entries in the 'Email' column, and outliers in both the 'Age' and 'Salary' columns, providing opportunities to apply a range of validation checks.

Checking for Missing Values

Missing values, denoted by NaN, can disrupt data analysis. Detecting and addressing them is vital for maintaining data quality.

Python
# Identifying missing values
missing_values = df.isnull().sum()
missing_columns = missing_values[missing_values > 0].index.tolist()

# Printing the missing columns for further investigation
if missing_columns:
    print(f"Missing values detected in columns: {missing_columns}")

# Using assert to raise an error if missing values are detected
assert missing_values.sum() == 0, f"Missing values detected in columns: {missing_columns}"

We use the .index attribute to retrieve the index labels of the missing_values Series where the condition missing_values > 0 is true. This effectively gives us the column names that have missing values. The tolist() method then converts these index labels into a Python list.

In this section, we create a list of column names that contain missing values. We first print the missing columns for further investigation. Additionally, using assert, we can raise an error if any missing values are detected, which is useful for debugging. Note that in this lesson, we use assertions to ensure data quality by halting execution when issues are detected.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal