w3resource

Ensuring column uniqueness in a Pandas DataFrame


Pandas: Data Validation Exercise-7 with Solution


Write a Pandas program to check that a specific column contains only unique values.

This exercise demonstrates how to ensure that a specific column contains only unique values by checking for duplicates.

Sample Solution :

Code :

import pandas as pd

# Create a sample DataFrame
df = pd.DataFrame({
    'ID': [1, 2, 3, 3],
    'Name': ['Orville', 'Arturo', 'Ruth', 'David']
})

# Check if the 'ID' column has unique values
unique_ids = df['ID'].is_unique

# Output the result
print(f"Is 'ID' column unique? {unique_ids}")

Output:

Is 'ID' column unique? False

Explanation:

  • Created a DataFrame with an 'ID' column that contains duplicate values.
  • Used is_unique to check if the 'ID' column contains only unique values.
  • Displayed whether the 'ID' column is unique.

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/ensure-column-uniqueness-in-a-dataframe-using-pandas.php