liveBook · Manning

Distinct value

As explained in Starting Data Analytics with Generative AI and Python

A unique value is a distinct value occurring in a field or variable, counted once regardless of its frequency.

How many customers are represented in an orders dataset? Before counting rows, decide which field identifies what you want to count. A distinct value is one different value in a field, counted once even if it appears in several rows. In pandas, `nunique()` counts distinct values; `value_counts()` shows each value and how often it occurs. section 2.3

Check two customer identifiers

Ask an AI assistant: “Show me pandas code to count distinct `customer_id` and `customer_unique_id` values, then count how many distinct `customer_id` values belong to each `customer_unique_id`.” Run the code and inspect its output rather than assuming the field names answer the business question. One analysis found 99,441 distinct `customer_id` values and 96,096 distinct `customer_unique_id` values; a single `customer_unique_id` was associated with as many as 17 distinct `customer_id` values. section 2.3

import pandas as pd

# Sample DataFrame
data = {'customer_id': [1, 2, 3, 4, 5],
        'customer_unique_id': [1001, 1002, 1002, 1004, 1004]}

df = pd.DataFrame(data)

# Check the number of unique values in each column
unique_customer_ids = df['customer_id'].nunique()
unique_customer_unique_ids = df['customer_unique_id'].nunique()

print(f"Unique customer_id values: {unique_customer_ids}")
print(f"Unique customer_unique_id values: {unique_customer_unique_ids}")

# Check the relationship between the two columns
relationship = df.groupby('customer_unique_id')['customer_id'].nunique()

print("\nRelationship between customer_unique_id and customer_id:")
print(relationship)

This pandas example uses `nunique()` for each field and `groupby()` to inspect how their values relate. section 2.3

A distinct-value count does not establish that the underlying records are unique. Check repeated values and sample records before deciding what to count or remove. In review data, a copying glitch could repeat one complaint many times and give it undue weight; slightly altered duplicates may need more than an exact-match check. section 2.3 section 5.2.1

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Demonstrates pandas counts and grouped identifier checks, but does not establish whether the identifiers denote different real-world customers.
Examples
Uses customer identifier columns and a grouped count; contrasts them with repeated customer reviews that could distort analysis.
What it assumes
Assumes a pandas DataFrame with columns, while showing the methods needed to inspect their values.
What you can do after
Apply it via pandas `nunique()`, `value_counts()`, and `groupby()` to inspect distinct identifiers and repeated values.
How it connects
Links distinct-value counts to categorical frequencies, identifier relationships, duplicate detection, and data quality.

Also explained in

Compare how 3 books explain distinct value →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Starting Data Analytics with Generative AI and Python. The section links point to the parts of the book it draws on.