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.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics