Data validation checks help us find mistakes or problems in data before using it. This keeps our results correct and trustworthy.
When you get new data and want to make sure it looks right.
Before analyzing data to catch missing or wrong values.
When combining data from different sources to check consistency.
To confirm data types are correct for calculations.
To find duplicates or unexpected values in a dataset.
Syntax
Pandas
import pandas as pd
# Check for missing values
missing = df.isnull()
# Check data types
types = df.dtypes
# Check for duplicates
duplicates = df.duplicated()
# Check if values meet a condition
condition_check = df['column_name'] > 0
df.isnull() returns True where data is missing.
df.duplicated() marks duplicate rows as True.
Examples
Counts how many missing values are in each column.
Pandas
df.isnull().sum()
Shows the data type of the 'age' column.
Pandas
df['age'].dtype
Counts how many duplicate rows are in the DataFrame.
Pandas
df.duplicated().sum()
Checks if all 'salary' values are between 30,000 and 150,000.
Pandas
df['salary'].between(30000, 150000).all()
Sample Program
This program creates a small table with some missing and duplicate data. It then checks for missing values, data types, duplicates, and if all ages are positive numbers.
Pandas
import pandas as pd
# Create sample data
data = {
'name': ['Alice', 'Bob', 'Charlie', 'David', 'Bob'],
'age': [25, 30, None, 22, 30],
'salary': [50000, 60000, 70000, None, 60000]
}
df = pd.DataFrame(data)
# Check for missing values
missing_counts = df.isnull().sum()
# Check data types
data_types = df.dtypes
# Check for duplicates
duplicate_rows = df.duplicated().sum()
# Check if all ages are positive
ages_positive = (df['age'] > 0).all()
print('Missing values per column:')
print(missing_counts)
print('\nData types:')
print(data_types)
print(f'\nNumber of duplicate rows: {duplicate_rows}')
print(f'\nAre all ages positive? {ages_positive}')
OutputSuccess
Important Notes
Missing values can cause errors in calculations if not handled.
Duplicates might skew your analysis and should be reviewed.
Always check data types to ensure correct operations.
Summary
Data validation checks help find missing, wrong, or duplicate data.
Use pandas functions like isnull(), duplicated(), and dtypes to check data.
Validating data early keeps your analysis accurate and reliable.
Practice
(1/5)
1. What does the pandas function isnull() check in a DataFrame?
easy
A. It checks for missing or null values in the data.
B. It checks for duplicate rows in the DataFrame.
C. It returns the data types of each column.
D. It sorts the DataFrame by a column.
Solution
Step 1: Understand the purpose of isnull()
The isnull() function in pandas returns a DataFrame of the same shape with True where values are missing (NaN) and False otherwise.
Step 2: Compare with other functions
Functions like duplicated() check duplicates, and dtypes shows data types, so they do not check for missing values.
Final Answer:
It checks for missing or null values in the data. -> Option A
Quick Check:
Missing data = isnull() [OK]
Hint: Missing values? Use isnull() to find them fast [OK]
Common Mistakes:
Confusing isnull() with duplicated()
Thinking isnull() returns data types
Assuming isnull() sorts data
2. Which of the following is the correct syntax to check for duplicate rows in a pandas DataFrame named df?
easy
A. df.isnull()
B. df.check_duplicates()
C. df.duplicated()
D. df.find_duplicates()
Solution
Step 1: Recall pandas method for duplicates
The correct pandas method to find duplicate rows is duplicated(), called as df.duplicated().
Step 2: Verify other options
isnull() checks missing data, and check_duplicates() and find_duplicates() are not valid pandas methods.
Final Answer:
df.duplicated() -> Option C
Quick Check:
Duplicates = duplicated() [OK]
Hint: Duplicates? Use df.duplicated() to spot them [OK]
Common Mistakes:
Using non-existent methods like check_duplicates()
Mixing isnull() with duplicate checks
Forgetting parentheses after duplicated
3. Given the DataFrame df below, what will df.isnull().sum() output?
Column 'A' has one None (missing), 'B' has one None, and 'C' has no missing values.
Step 2: Understand what isnull().sum() does
isnull() marks missing as True, and sum() counts True values per column.
Final Answer:
{'A': 1, 'B': 1, 'C': 0} -> Option D
Quick Check:
Count missing per column = isnull().sum() [OK]
Hint: Sum isnull() to count missing per column [OK]
Common Mistakes:
Counting total missing instead of per column
Confusing None with zero
Misreading DataFrame values
4. The following code is intended to find duplicate rows in df. What is the error?
duplicates = df.duplicate()
medium
A. The method name should be duplicated(), not duplicate().
B. The DataFrame df is not defined.
C. The method duplicated() requires an argument.
D. The code should use df.isnull() to find duplicates.
Solution
Step 1: Check method name correctness
The correct pandas method to find duplicates is duplicated(), not duplicate().
Step 2: Verify other options
df is assumed defined, duplicated() does not require arguments, and isnull() checks missing data, not duplicates.
Final Answer:
The method name should be duplicated(), not duplicate(). -> Option A
Quick Check:
Correct method name = duplicated() [OK]
Hint: Remember: duplicated() has a 'd' at the end [OK]
Common Mistakes:
Misspelling duplicated() as duplicate()
Confusing duplicated() with isnull()
Thinking duplicated() needs arguments
5. You have a DataFrame df with columns ['id', 'age', 'score']. You want to validate that age has no missing values and score has no duplicates. Which code correctly performs both checks?