> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Count the Occurrences of a Specific Value in Pandas DataFrame
- URL: https://datascientyst.com/how-to-count-the-occurrences-of-a-specific-value-in-pandas-dataframe/
- Published: 2025-02-17T22:18:07.000Z
- Updated: 2025-02-17T22:18:29.000Z
- Author: John D K
- Tags: count()

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**

```python
df['column_name'].value_counts().get('target_value', 0)

```

**(2) Count occurrences across the entire DataFrame**

```python
(df == 'target_value').sum().sum()

```

**(3) Count occurrences using `.shape`**

```python
df[df['column_name'] == 'target_value'].shape[0]

```

**(4) Count occurrences using `len()`**

```python
len(df[df['column_name'] == 'target_value'])

```

**(5) Count occurrences using `.query()`**

```python
df.query('column_name == "target_value"').column_name.count()

```

**(6) Count occurrences using boolean mask with `.sum()`**

```python
(df['column_name'] == 'target_value').sum()

```

**(7) Count occurrences using NumPy array**

```python
(df['column_name'].values == 'target_value').sum()

```

![](https://datascientyst.com/content/images/2025/02/how-to-count-the-occurrences-of-a-specific-value-in-pandas-dataframe.webp)

## 1: Example DataFrame

Let's create a sample DataFrame:

```python
import pandas as pd

data = {
    'A': ['apple', 'banana', 'apple', 'orange', 'banana'],
    'B': ['apple', 'apple', 'banana', 'banana', 'apple']
}

df = pd.DataFrame(data)

```

### **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`:

```python
df['A'].value_counts().get('apple', 0)

```

### **Output:**

```
2

```

## 3: Count Occurrences Across the Entire DataFrame

To count `"apple"` occurrences in all columns:

```python
(df == 'apple').sum().sum()

```

### **Output:**

```
4

```

## 4: Count Multiple Values

If you want to count multiple values in a column:

```python
df['A'].value_counts()

```

### **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

## Resources

- [Pandas .value\_counts() Documentation](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.value%5Fcounts.html?ref=datascientyst.com)
- [Pandas Boolean Indexing](https://pandas.pydata.org/docs/user%5Fguide/indexing.html?ref=datascientyst.com#boolean-indexing)
- [Pandas Summing Boolean Values](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.sum.html?ref=datascientyst.com)
- [Python Pandas Counting the Occurrences of a Specific value](https://datascientyst.com/questions/35277075/python-pandas-counting-the-occurrences-of-a-specific-value)