w3resource

Ensuring no missing values in a critical column using Pandas

Pandas: Data Validation Exercise-10 with Solution

Write a Pandas program that ensures no missing values in a critical column.

This exercise demonstrates how to ensure that a critical column (e.g., 'ID') has no missing values using notna().

Sample Solution :

Code :

import pandas as pd

# Create a sample DataFrame with missing values in the 'ID' column
df = pd.DataFrame({
    'ID': [1, 2, None, 4],
    'Name': ['Orville', 'Arturo', 'Ruth', 'David']
})

# Check if the 'ID' column has any missing values
missing_ids = df['ID'].notna().all()

# Output the result
print(f"Are there any missing IDs? {not missing_ids}")

Output:

Are there any missing IDs? True

Explanation:

  • Created a DataFrame where the 'ID' column contains missing values.
  • Used notna().all() to check if there are any missing values in the 'ID' column.
  • Outputted whether any missing values are present.

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-ensure-no-missing-values-in-a-critical-column.php