A Common Pandas Mistake: groupby Drops Rows With Missing Values
groupby() drops rows whose grouping column is empty, so a total can quietly shrink between one grouping and the next with nothing warning you. Pass dropna=False to keep them, and check for missing values with isna().sum() before you group anything.
Why the total changes
The total changes because groupby() silently drops every row whose grouping column is empty. To show it, I use an Excel file with financial data for a business, with total sales amounting to 17 million USD. Grouped by segment, the total holds; grouped by segment and product, part of it disappears.
To load the data into a DataFrame I use my MyToolBelt add-in for this, which turns a selected range into a notebook with the data loaded. For a quick one-off you can also create the DataFrame from the clipboard.

MyToolBelt creates a Jupyter notebook with the data already loaded. The first rows match the spreadsheet, and the sales add up to the expected 17 million.
df.head()
Segment Country Product Discount Band Units Sold Sale Price Gross Sales Discounts Sales COGS Profit
0 Midmarket Japan Carretera NaN 2470 150 370500 0.0 370500.0 24700 345800.0
1 Midmarket Japan Montana NaN 2470 15 37050 0.0 37050.0 24700 12350.0
2 Small Business Japan Montana NaN 958 300 287400 0.0 287400.0 239500 47900.0
3 Midmarket Japan Paseo NaN 974 15 14610 0.0 14610.0 9740 4870.0
4 Government Japan Paseo NaN 883 7 6181 0.0 6181.0 4415 1766.0
df["Sales"].sum()
17000000.0
Grouped by segment, the sales still add up to 17 million:
by_segment = df.groupby(by=["Segment"])[["Sales"]].sum()
by_segment.sum()
Sales 17000000.0
dtype: float64
Grouped by segment and product, the same sum drops to 15.8 million, and pandas gives no warning:
by_segment_and_product = df.groupby(by=["Segment", "Product"])[["Sales"]].sum()
by_segment_and_product.sum()
Sales 15815900.21
dtype: float64
Keep missing values with dropna=False
By default, groupby() drops every row whose grouping column is empty. Two rows in this file have no product, so they vanish from the second grouping. Set dropna to False (available since pandas 1.1) to keep them:
by_segment_and_product = df.groupby(by=["Segment", "Product"], dropna=False)[["Sales"]].sum()
by_segment_and_product.sum()
Sales 17000000.0
dtype: float64
The total is back at 17 million. The rows with an empty product now form their own group with NaN as the label: two Government rows without a product name, worth about 1.18 million in sales.
Check for missing values first
A one-line check before any groupby shows which columns have gaps. isna() marks every missing value as True, and sum() counts them per column:
df.isna().sum()
Segment 0
Country 0
Product 2
Discount Band 7
Units Sold 0
Sale Price 0
Gross Sales 0
Discounts 0
Sales 0
COGS 0
Profit 0
dtype: int64
Product has the two missing values that caused the gap. The 7 in Discount Band is a pandas 2 effect: since pandas 2.0, read_excel also treats the text “None” as missing, and this file uses “None” for rows without a discount. The video, recorded with an older pandas, shows 0 there. If “None” is a real category in your data, read the file with keep_default_na=False, na_values=[""] so only empty cells count as missing.
For the share of missing values instead of the count, take the mean of isna() and multiply by 100:
df.isna().mean().mul(100)
Segment 0.000000
Country 0.000000
Product 1.904762
Discount Band 6.666667
Units Sold 0.000000
Sale Price 0.000000
Gross Sales 0.000000
Discounts 0.000000
Sales 0.000000
COGS 0.000000
Profit 0.000000
dtype: float64
Here 1.9% of the Product values are missing. If you group by a column with gaps, decide first whether those rows belong in their own group (dropna=False), should be filled with fillna(), or really should be left out. I run the same check at the start of every analysis, as in my complete pandas tutorial, and more groupby options are in four pandas functions I wish I knew earlier.
