Counting What's There: value_counts and unique

value_counts counts how many rows hold each value of a column, most common first, normalize=True turns the counts into shares of the whole, unique lists the different values in a column and nunique counts them.

Apprentice · Python · Updated

By

After this lesson you can

  • Count how often each value appears in a column with value_counts, as counts or as shares with normalize=True
  • List the different values in a column with unique, and count them with nunique

Key takeaways

  • value_counts() counts how many rows hold each value in a column, most common first. It counts rows, not the amounts in another column.
  • value_counts(normalize=True) gives each value's share of the rows, from 0 to 1. Multiply by 100 for percentages.
  • unique() lists the different values in a column, in the order they first appear, and nunique() counts them.
  • Counting answers the first questions about any table, such as which category is most common or how many vendors there are, and it shows spellings you didn't expect.

"How many of each?" is the first question people ask about almost any table: how many orders came from each store, how many survey answers said yes, which kind of ticket the help desk sees most. Counting also catches surprises, such as a category spelled two ways. In pandas it's one method, and you'll use it in nearly every notebook.

The example is Sunnyside Farmers Market's sales for the 2026 season, sales.csv: one row for each product a vendor sold on a Saturday, 1,900 rows, with the columns date, vendor_id, product, category, units and price. The market is our own made-up data.

How many of each: value_counts

Load the file (pandas is imported as pd, read "p d") and pick the category column. value_counts() counts how many rows hold each value, most common first:

import pandas as pd

sales = pd.read_csv("sales.csv")
sales["category"].value_counts()
category
produce        654
baked goods    415
crafts         399
honey          270
flowers        162
Name: count, dtype: int64

Produce shows up in 654 rows, flowers in 162. Read that carefully: it counts rows, and a row is one product on one Saturday. So 654 is how many times a produce item was on the sales sheet, not how many vegetables were sold; adding up the units column by category is a different question, which Grouping with groupby answers. VS Code shows the result as a grid; this lesson shows it as pandas prints it as text.

Shares of the whole: normalize=True

Counts are hard to compare across tables of different sizes. normalize=True turns each count into its share of all the rows, from 0 to 1:

sales["category"].value_counts(normalize=True).round(3)
category
produce        0.344
baked goods    0.218
crafts         0.210
honey          0.142
flowers        0.085
Name: proportion, dtype: float64

A share of 0.344 is 34.4%: about a third of the rows are produce. Multiply by 100 for percentages, (sales["category"].value_counts(normalize=True) * 100).round(1). The shares always add up to 1, because every row is in exactly one category.

Which values are there: unique and nunique

Sometimes you want the list, not the counts. unique() gives each different value once, in the order it first appears:

sales["category"].unique()
<StringArray>
['produce', 'honey', 'baked goods', 'flowers', 'crafts']
Length: 5, dtype: str

That's an array, pandas' list-like of values, with its length and dtype underneath. When you only need how many, nunique() ("n unique", the number of unique values) counts them:

print("categories:", sales["category"].nunique())
print("vendors:", sales["vendor_id"].nunique())
print("products:", sales["product"].nunique())
print("Saturdays:", sales["date"].nunique())
categories: 5
vendors: 15
products: 80
Saturdays: 31

Four questions, four lines: 15 vendors sold 80 different products over 31 Saturdays. On a messy file, unique() is where you'd spot Produce and produce as two categories, one category spelled two ways that cleaning has to make match.

The first questions they answer

value_counts works on any column, and head or tail keeps the list short. Which vendors appear most often on the sales sheet?

sales["vendor_id"].value_counts().head(3)
vendor_id
V03    180
V01    171
V09    160
Name: count, dtype: int64

Rise & Shine Bakery (V03), with six products every week, has the most rows. And the products:

sales["product"].value_counts().head(5)
product
honey jar       60
honey sticks    60
lettuce         31
kale            31
spinach         31
Name: count, dtype: int64

A season has 31 Saturdays, so how can honey jars appear 60 times? Two vendors sell them, Bee Happy Honey and Golden Comb Apiary, and a count twice the number of Saturdays is the clue. A count that looks impossible is usually telling you something true about the data.

Check yourself

2 questions to try on the lesson page.

So what

value_counts, unique and nunique answer "how many of each?" and "how many different?" in one line each, and the counts that surprise you are where a table's story starts.

Key terms

Related