Counting Groups
size counts the rows in each group. count counts the values that are present in each column.
Count rows per group
size counts every row in each group. Drink has 2 rows, and Home has 1 row.
import pandas as pd
df = pd.DataFrame({
"product": ["Tea", "Coffee", "Mug"],
"category": ["Drink", "Drink", "Home"],
"region": ["North", None, "East"],
})
print(df.groupby("category").size())Count values per column
count skips the missing region. Drink still has 2 rows, but only 1 region value.
import pandas as pd
df = pd.DataFrame({
"product": ["Tea", "Coffee", "Mug"],
"category": ["Drink", "Drink", "Home"],
"region": ["North", None, "East"],
})
print(df.groupby("category").count())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.groupby("region").size())
print(sales.groupby("category").size())Key points
- Use size after groupby to count the rows in each group.
- Use count after groupby to count the values in each column.
- count skips missing values. size counts every row, missing or not.
- The result has one row per group.
In this example, the sales table loads from the CSV text. size counts the rows in each region group, then in each category group. Every region holds 3 orders, and Office holds 4 orders.
Note
A group with a missing value shows a smaller count than size for that column.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
A group has 4 rows, and one value is missing. What does count give for that column?