w3resource

Validating Data Using Custom Conditions in a Pandas DataFrame


6. Validating Data Based on Custom Conditions

Write a Pandas program to validate data based on custom conditions.

In this exercise, we have validated data based on custom conditions, such as ensuring that all values in a column are within a specified range.

Sample Solution :

Code :

import pandas as pd

# Create a sample DataFrame
df = pd.DataFrame({
    'Age': [25, 30, 22, 15],
    'Salary': [50000, 60000, 70000, 40000]
})

# Define a custom validation condition (Age must be between 18 and 65)
valid_ages = df['Age'].between(18, 65)

# Output the result
print(valid_ages)

Output:

0     True
1     True
2     True
3    False
Name: Age, dtype: bool

Explanation:

  • Created a DataFrame with 'Age' and 'Salary' columns.
  • Used between() to validate that all ages are between 18 and 65.
  • Outputted a Boolean Series indicating whether each value in the 'Age' column meets the condition.

For more Practice: Solve these Related Problems:

  • Write a Pandas program to validate that numeric columns meet a specific threshold and output rows that violate the condition.
  • Write a Pandas program to apply multiple custom conditions to a DataFrame and generate a report of rows that fail any condition.
  • Write a Pandas program to validate data by checking if values in a column fall within a custom range and mark the violations.
  • Write a Pandas program to validate a DataFrame based on custom string patterns in a column and list non-matching rows.

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.