# count the frequency that a value occurs in a dataframe column

1 view

I have a dataset

|category|

cat a

cat b

cat a

I'd like to be able to return something like (showing unique values and frequency)

category | freq |

cat a       2

cat b       1

by (33.1k points)

There are many ways to solve this problem:

Use groupby() and count() functions:

In [37]:

df = pd.DataFrame({'a':list('abssbab')})

df.groupby('a').count()

Out[37]:

a  a

a  2

b  3

s  2

Use value_counts() method:

In [38]:

df['a'].value_counts()

Out[38]:

b   3

a   2

s   2

dtype: int64

Use transform() method:

In [41]:

df['freq'] = df.groupby('a')['a'].transform('count')

df

Out[41]:

a freq

0  a 2

1  b 3

2  s 2

3  s 2

4  b 3

5  a 2

6  b 3

by (107k points)

To count the frequency that a value occurs in a dataframe column if you want to apply to all columns then you can use the below-mentioned code:-

df.apply(pd.value_counts)

This will apply a column-based aggregation function (in this case value_counts) to each of the columns.