w3resource

Validating Data Using Custom Conditions in a Pandas DataFrame

Pandas: Data Validation Exercise-6 with Solution

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.

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://w3resource.com/python-exercises/pandas/validate-data-using-custom-conditions-in-a-pandas-dataframe.php