w3resource

Verifying numeric range in a column using Pandas

Pandas: Data Validation Exercise-12 with Solution

Write a Pandas program to verify numeric range in a DataFrame column.

This exercise demonstrates how to verify that all values in a numerical DataFrame column fall within a specific range.

Sample Solution :

Code :

import pandas as pd

# Create a sample DataFrame with numeric data
df = pd.DataFrame({
    'Score': [85, 90, 95, 100, 110]
})

# Verify that all scores are between 0 and 100
valid_scores = df['Score'].between(0, 100)

# Output the result
print(valid_scores)

Output:

0     True
1     True
2     True
3     True
4    False
Name: Score, dtype: bool

Explanation:

  • Created a DataFrame with 'Score' values.
  • Used between() to check that all scores fall between 0 and 100.
  • Outputted a Boolean Series indicating whether each score meets the range 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/pandas-verify-numeric-range-in-a-column.php