*23*

You can use the **value_counts()** function to count the frequency of unique values in a pandas Series.

This function uses the following basic syntax:

my_series.value_counts()

The following examples show how to use this syntax in practice.

**Example 1: Count Frequency of Unique Values**

The following code shows how to count the occurrences of unique values in a pandas Series:

import pandas as pd #create pandas Series my_series = pd.Series([3, 3, 3, 3, 4, 4, 7, 7, 8, 9]) #count occurrences of unique values in Series my_series.value_counts() 3 4 4 2 7 2 8 1 9 1 dtype: int64

This tells us:

- The value 3 occurs
**4**times. - The value 4 occurs
**2**times. - The value 7 occurs
**2**times.

And so on.

**Example 2: Count Frequency of Unique Values (Including NaNs)**

By default, the **value_counts()** function does not show the frequency of NaN values.

However, you can use the **dropna** argument to display the frequency of NaN values:

import pandas as pd import numpy as np #create pandas Series with some NaN values my_series = pd.Series([3, 3, 3, 3, 4, 4, 7, 7, 8, 9, np.nan, np.nan]) #count occurrences of unique values in Series, including NaNs my_series.value_counts(dropna=False) 3.0 4 4.0 2 7.0 2 NaN 2 8.0 1 9.0 1 dtype: int64

**Example 3: Count Relative Frequency of Unique Values**

The following code shows how to use the **normalize** argument to count the relative frequency of unique values in a pandas Series:

import pandas as pd #create pandas Series my_series = pd.Series([3, 3, 3, 3, 4, 4, 7, 7, 8, 9]) #count occurrences of unique values in Series my_series.value_counts(normalize=True) 3 0.4 4 0.2 7 0.2 8 0.1 9 0.1 dtype: float64

This tells us:

- The value 3 represents
**40%**Â of all values in the Series. - The value 4 represents
**20%**Â of all values in the Series. - The value 7 represents
**20%**of all values in the Series.

And so on.

**Example 4: Count Frequency in Bins**

The following code shows how to use the **bins** argument to count the frequency of values in a pandas Series that fall into equal-sized bins:

import pandas as pd #create pandas Series my_series = pd.Series([3, 3, 3, 3, 4, 4, 7, 7, 8, 9]) #count occurrences of unique values in Series my_series.value_counts(bins=3) (3.0, 5.0] 6 (5.0, 7.0] 2 (7.0, 9.0] 2 dtype: int64

This tells us:

- There are
**6**values that fall in the range 3 to 5. - There are
**2**values that fall in the range 5 to 7. - There are
**2**values that fall in the range 7 to 9.

**Example 5: Count Frequency of Values in Pandas DataFrame**

We can also use the **value_counts()** function to calculate the frequency of unique values in a specific column of a pandas DataFrame:

import pandas as pd #create DataFrame df = pd.DataFrame({'points': [9, 9, 9, 10, 10, 13, 15, 22], 'assists': [5, 7, 7, 9, 12, 9, 9, 4], 'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]}) #count occurrences of unique values in 'points' column df['points'].value_counts() 9 3 10 2 13 1 15 1 22 1 Name: points, dtype: int64

**Additional Resources**

The following tutorials explain how to use other common functions in pandas:

How to Use describe() Function in Pandas

How to Count Number of Rows in Pandas

How to Count Observations by Group in Pandas