Unlike len(df) or df.shape[0], which report the total number of rows regardless of missing data, count() specifically counts only the non-null values in each column, so a column with missing entries reports a smaller count than the DataFrame's total row count. Chained after groupby(), count() reports how many non-null values each group has per column, which is a quick way to spot groups with incomplete data.
1Understanding df.count()
Unlike len(df) or df.shape[0], which report the total number of rows regardless of missing data, count() specifically counts only the non-null values in each column, so a column with missing entries reports a smaller count than the DataFrame's total row count. Chained after groupby(), count() reports how many non-null values each group has per column, which is a quick way to spot groups with incomplete data.
Don't confuse df.count() with len(df) — count() reports non-null values per column, which can differ between columns and from the total row count, while len(df) always reports the DataFrame's overall row count regardless of any missing data.
import pandas as pd
import numpy as np
df = pd.DataFrame({"a": [1, 2, np.nan, 4], "b": [1, 2, 3, 4]})
print(df.count())2Practical Example
Here is a real-world application of df.count() showing how it is used in production Pandas code.
import pandas as pd
df = pd.DataFrame({"team": ["A", "A", "B"], "score": [10, 20, 30]})
print(df.groupby("team")["score"].count())3Best Practices
Follow these guidelines when working with df.count():
1. Use count() (not len()) specifically when you need to know how much actual, non-missing data exists in each column
2. Compare count() against the DataFrame's total row count to quickly spot which columns have missing values, and how many
3. Use count() after groupby() to check for groups with unexpectedly incomplete data, rather than assuming every group is equally complete
Tip: Don't confuse df.count() with len(df) — count() reports non-null values per column, which can differ between columns and from the total row count, while len(df) always reports the DataFrame's overall row count regardless of any missing data.
import pandas as pd
import numpy as np
df = pd.DataFrame({"a": [1, 2, np.nan, 4], "b": [1, 2, 3, 4]})
print(df.count())