Skip to content
TheChatArchiveThe Chat Archive
Mistralmistral-medium-2505· Mistral AIHugging Face LMArena Human Preference 140K

Suggest a data validation script that checks if 'age' column values are between 0 and…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Coherecommand-a-03-2025’s answer.
  1. User

    Suggest a data validation script that checks if 'age' column values are between 0 and 120 and of integer type.

  2. Mistral Assistant

    Data Validation Script for Age Column

    Here's a Python script that validates an 'age' column to ensure all values are integers between 0 and 120:

    import pandas as pd
    
    def validate_age_column(data, age_column='age'):
        """
        Validate that all values in the age column are integers between 0 and 120.
    
        Args:
            data: pandas DataFrame containing the data
            age_column: name of the column to validate (default: 'age')
    
        Returns:
            tuple: (bool, str) where bool indicates validation success,
                   and str contains error message if validation fails
        """
        # Check if column exists
        if age_column not in data.columns:
            return False, f"Column '{age_column}' not found in the dataset"
    
        # Check for non-integer values
        non_integer_mask = ~data[age_column].apply(lambda x: isinstance(x, (int, float)) and x == int(x))
        non_integer_values = data.loc[non_integer_mask, age_column]
    
        if not non_integer_values.empty:
            return False, f"Non-integer values found in age column: {non_integer_values.tolist()}"
    
        # Check for values outside 0-120 range
        out_of_range_mask = (data[age_column] < 0) | (data[age_column] > 120)
        out_of_range_values = data.loc[out_of_range_mask, age_column]
    
        if not out_of_range_values.empty:
            return False, f"Age values out of range (0-120): {out_of_range_values.tolist()}"
    
        # Check for missing values
        missing_values = data[age_column].isna().sum()
        if missing_values > 0:
            return False, f"Missing values found in age column: {missing_values}"
    
        return True, "Age column validation passed successfully"
    
    # Example usage:
    if __name__ == "__main__":
        # Sample data
        sample_data = pd.DataFrame({
            'age': [25, 30, 45, 150, 22.5, None, -5, 100, 75]
        })
    
        # Run validation
        is_valid, message = validate_age_column(sample_data)
    
        print(f"Validation result: {'Success' if is_valid else 'Failed'}")
        print(message)
    

    Alternative Version (for non-pandas data)

    If you're not using pandas, here's a simpler version:

    def validate_age_values(age_list):
        """
        Validate a list of age values.
    
        Args:
            age_list: list of age values to validate
    
        Returns:
            tuple: (bool, str) where bool indicates validation success,
                   and str contains error message if validation fails
        """
        errors = []
    
        for i, age in enumerate(age_list):
            # Check for None or NaN values
            if age is None or (isinstance(age, float) and pd.isna(age)):
                errors.append(f"Missing value at position {i}")
                continue
    
            # Check if value is integer
            if not isinstance(age, int) or isinstance(age, bool):
                errors.append(f"Non-integer value at position {i}: {age}")
                continue
    
            # Check age range
            if age < 0 or age > 120:
                errors.append(f"Age out of range (0-120) at position {i}: {age}")
    
        if errors:
            return False, "\n".join(errors)
        return True, "All age values are valid"
    

    This script:

    1. Checks if the column exists
    2. Verifies all values are integers (or floats that are equivalent to integers)
    3. Ensures all values are between 0 and 120
    4. Checks for missing values
    5. Returns a tuple with validation status and message

    You can modify the column name parameter if your age column has a different name.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.