Python - Calculate the count of column values of a Pandas DataFrame

To calculate the count of column values in a Pandas DataFrame, use the count() method. This method returns the number of non-null values in each column, making it useful for data validation and analysis.

Importing Required Library

First, import the Pandas library ?

import pandas as pd

Counting Values in a Specific Column

You can count non-null values in a specific column by accessing the column and applying count() ?

import pandas as pd

# Create DataFrame1
dataFrame1 = pd.DataFrame(
   {
      "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'],
      "Units": [100, 150, 110, 80, 110, 90]
   }
)

print("DataFrame1:")
print(dataFrame1)

# Finding count of values of a specific column
print("\nCount of values of Units column =", dataFrame1['Units'].count())
DataFrame1:
       Car  Units
0      BMW    100
1    Lexus    150
2     Audi    110
3    Tesla     80
4  Bentley    110
5   Jaguar     90

Count of values of Units column = 6

Counting Values in All Columns

To get the count of non-null values for all columns, call count() on the entire DataFrame ?

import pandas as pd

# Create DataFrame2
dataFrame2 = pd.DataFrame(
   {
      "Product": ['TV', 'PenDrive', 'HeadPhone', 'EarPhone', 'HDD'],
      "Price": [8000, 500, 3000, 1500, 3000]
   }
)

print("DataFrame2:")
print(dataFrame2)

# Finding count of values of all columns
print("\nCount of column values:")
print(dataFrame2.count())
DataFrame2:
   Product  Price
0       TV   8000
1  PenDrive    500
2 HeadPhone   3000
3  EarPhone   1500
4      HDD   3000

Count of column values:
Product    5
Price      5
dtype: int64

Handling Missing Values

The count() method is particularly useful when dealing with DataFrames containing missing values ?

import pandas as pd
import numpy as np

# Create DataFrame with missing values
df_with_nulls = pd.DataFrame(
   {
      "A": [1, 2, np.nan, 4, 5],
      "B": [10, np.nan, 30, np.nan, 50],
      "C": [100, 200, 300, 400, 500]
   }
)

print("DataFrame with missing values:")
print(df_with_nulls)

print("\nCount of non-null values:")
print(df_with_nulls.count())
DataFrame with missing values:
     A     B      C
0  1.0  10.0  100.0
1  2.0   NaN  200.0
2  NaN  30.0  300.0
3  4.0   NaN  400.0
4  5.0  50.0  500.0

Count of non-null values:
A    4
B    3
C    5
dtype: int64

Key Points

  • count() excludes NaN (null) values from the count
  • Use df['column'].count() for a specific column
  • Use df.count() for all columns at once
  • Returns a Series when applied to the entire DataFrame

Conclusion

The count() method is essential for data quality assessment, helping you identify columns with missing data. Use it on specific columns or the entire DataFrame to get non-null value counts efficiently.

Updated on: 2026-03-26T02:08:54+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements