In this short guide, I'll show you how to count occurrences of a specific value in Pandas. Whether you want to count values in a single column or across the whole DataFrame, this guide has you covered.
(1) Count occurrences in a single column
(2) Count occurrences across the entire DataFrame
(3) Count occurrences using .shape
(4) Count occurrences using len()
(5) Count occurrences using .query()
(6) Count occurrences using boolean mask with .sum()
(7) Count occurrences using NumPy array

1: Example DataFrame
Let's create a sample DataFrame:
Output:
| A | B | |
|---|---|---|
| 0 | apple | apple |
| 1 | banana | apple |
| 2 | apple | banana |
| 3 | orange | banana |
| 4 | banana | apple |
2: Count Occurrences in a Column
To count occurrences of "apple" in column A:
Output:
2
3: Count Occurrences Across the Entire DataFrame
To count "apple" occurrences in all columns:
Output:
4
4: Count Multiple Values
If you want to count multiple values in a column:
Output:
banana 2
apple 2
orange 1
Name: A, dtype: int64
Conclusion
In this guide, we covered:
- Counting occurrences of a value in a specific column
- Counting occurrences across the entire DataFrame
- Using
.value_counts()for frequency analysis