> ## 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 Values in Pandas DataFrame
- URL: https://datascientyst.com/count-values-pandas-dataframe/
- Published: 2022-09-22T14:10:15.000Z
- Updated: 2022-09-22T14:10:15.000Z
- Author: John D K
- Tags: count()

In this tutorial, we're going to **count values in Pandas DataFrame**.

Check this article for most common values in DataFrame: [Get most frequent values in Pandas DataFrame](https://datascientyst.com/get-most-frequent-values-pandas-dataframe/)

## Setup

Suppose we have a dataframe with data:

```python
import pandas as pd
data = [('A',1, 0, 3, 1),
        ('A',1, 2, 5, 1),
        ('B',2, 1, 4, 3),
       ('B',3, 1, 0, 3),
       ('C',4, 3, 1, 2)]
cols = ('col_1', 'col_2', 'col_3', 'col_4', 'col_5' )
df = pd.DataFrame(data,
                 columns = cols)

```

data:

|   | col\_1 | col\_2 | col\_3 | col\_4 | col\_5 |
| - | ------ | ------ | ------ | ------ | ------ |
| 0 | A      | 1      | 0      | 3      | 1      |
| 1 | A      | 1      | 2      | 5      | 1      |
| 2 | B      | 2      | 1      | 4      | 3      |
| 3 | B      | 3      | 1      | 0      | 3      |
| 4 | C      | 4      | 3      | 1      | 2      |

## Step 1: Count values in Pandas Column

To **count values in single Pandas column** we can use method `value_counts()`:

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

```

The result is count of most frequent values sorted in ascending order:

```
A    2
B    2
C    1
Name: col_1, dtype: int64

```

## Step 2: Count values in Multiple columns

**Count values in multiple Pandas columns** can be down with method `.value_counts()`:

```python
df[['col_1', 'col_2']].value_counts()

```

The result is is most common values per group/columns:

```
col_1  col_2
A      1        2
B      2        1
       3        1
C      4        1
dtype: int64

```

## Step 3: Count values in Pandas DataFrame

To find the **most common values in whole DataFrame** we can combine:

- `melt`
- `value_counts`

```python
df.melt()['value'].value_counts()

```

So we get the\*\* count of all values for this DataFrame\*\*:

```
1    7
3    5
2    3
A    2
B    2
4    2
0    2
C    1
5    1
Name: value, dtype: int64

```

![](https://datascientyst.com/content/images/2022/09/count-values-pandas-dataframe-column.png)

## Conclusion

In this short article we covered **how to count values in Pandas DataFrame**. We saw how to count values in single or multiple columns.

Finally with covered how to get most common values for the whole DataFrame