🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.count()

AI & DATA SCIENCE // df-count

df.count() returns the number of non-null (non-missing) values in each column, or, after a groupby(), the count per group.

Syntax

df.count()

Deep Dive Course

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.

editor.html
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())
localhost:3000

2Practical Example

Here is a real-world application of df.count() showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"team": ["A", "A", "B"], "score": [10, 20, 30]})
print(df.groupby("team")["score"].count())
localhost:3000

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.

editor.html
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())
localhost:3000

Examples

Example 01Basic Usage
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())
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"team": ["A", "A", "B"], "score": [10, 20, 30]})
print(df.groupby("team")["score"].count())

Best Practices

  • Use count() (not len()) specifically when you need to know how much actual, non-missing data exists in each column
  • Compare count() against the DataFrame's total row count to quickly spot which columns have missing values, and how many
  • Use count() after groupby() to check for groups with unexpectedly incomplete data, rather than assuming every group is equally complete

Interview Question

Why might df.count() report a different number for two different columns in the same DataFrame?

Hint: Think about what count() actually measures, compared to a DataFrame's overall row count.

count() measures how many non-null values each individual column contains, and different columns can have different numbers of missing values — one column might be completely filled in for every row, while another has several NaN entries scattered through it. Since count() reports that number independently per column, rather than the DataFrame's single overall row count, which is the same for every column by definition, two columns in the same DataFrame can legitimately report different counts if they have different amounts of missing data.

Exercises

MediumPractice using df.count() in a real scenario.
View Solution
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())

Frequently Asked Questions

Why might df.count() report a different number for two different columns in the same DataFrame?

count() measures how many non-null values each individual column contains, and different columns can have different numbers of missing values — one column might be completely filled in for every row, while another has several NaN entries scattered through it. Since count() reports that number independently per column, rather than the DataFrame's single overall row count, which is the same for every column by definition, two columns in the same DataFrame can legitimately report different counts if they have different amounts of missing data.

Related Functions

df-isnadf-groupbydf-sum