🎓 A1Learner
🔍
0 of 50 lessons completed

Value Counts

The value_counts method counts how often each value appears. It turns a list of values into a small table of counts.

Count categories

This Series holds five category words. Drink appears three times, so it comes first.

import pandas as pd

categories = pd.Series(["Drink", "Drink", "Home", "Office", "Drink"])
print(categories.value_counts())

Count products

Tea appears three times in this Series. Coffee and Mug appear once each.

import pandas as pd

products = pd.Series(["Tea", "Coffee", "Tea", "Mug", "Tea"])
print(products.value_counts())

Count a table column

The subset option names the column to count. The sales table holds four orders in each category.

import io
import pandas as pd

SALES_CSV = """order_id,date,product,category,units,price,region
101,2026-01-05,Tea,Drink,2,4.50,North
102,2026-01-06,Coffee,Drink,1,6.00,South
103,2026-01-07,Mug,Home,3,12.00,East
104,2026-01-08,Notebook,Office,2,8.50,West
105,2026-01-09,Pen,Office,5,2.00,North
106,2026-01-12,Lamp,Home,1,24.00,South
107,2026-01-13,Tea,Drink,4,4.50,East
108,2026-01-14,Coffee,Drink,2,6.00,West
109,2026-01-15,Mug,Home,1,12.00,North
110,2026-01-16,Notebook,Office,3,8.50,South
111,2026-01-19,Pen,Office,2,2.00,East
112,2026-01-20,Lamp,Home,2,24.00,West"""

sales = pd.read_csv(io.StringIO(SALES_CSV))
print(sales.value_counts(subset=["category"]))

Example

import io
import pandas as pd

SALES_CSV = """order_id,date,product,category,units,price,region
101,2026-01-05,Tea,Drink,2,4.50,North
102,2026-01-06,Coffee,Drink,1,6.00,South
103,2026-01-07,Mug,Home,3,12.00,East
104,2026-01-08,Notebook,Office,2,8.50,West
105,2026-01-09,Pen,Office,5,2.00,North
106,2026-01-12,Lamp,Home,1,24.00,South
107,2026-01-13,Tea,Drink,4,4.50,East
108,2026-01-14,Coffee,Drink,2,6.00,West
109,2026-01-15,Mug,Home,1,12.00,North
110,2026-01-16,Notebook,Office,3,8.50,South
111,2026-01-19,Pen,Office,2,2.00,East
112,2026-01-20,Lamp,Home,2,24.00,West"""

sales = pd.read_csv(io.StringIO(SALES_CSV))
print(sales.value_counts(subset=["category"]))
print(sales.value_counts(subset=["region"]))

Key points

  • Use value_counts on a Series to count each value in it.
  • The most common value comes first.
  • The result pairs each value with its count.
  • Use value_counts to find the most common word in a list of words.

In this example, the sales CSV is read into a DataFrame named sales. The first print counts the orders in each category, and each category holds four orders. The second print counts the orders in each region, and each region holds three orders.

Note

The counts add up to the number of values in the Series.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

What does value_counts give back?