# How to calculate relative frequency in Python?

By Shivang Yadav Last updated : November 21, 2023

## Relative Frequency

Relative frequency is the count of occurrences of a value in a dataset relative to the total number of values present in the dataset.

Python provides multiple methods to calculate the relative frequency.

## Calculation of Relative Frequency

As we know it is the portion of total frequency taken by the value, it can be easily calculated as:

```Relative Frequency = (count of  value's occurrence)/(Total occurrence )
```

For this, we will be simply using a counter for counting occurrence and length method to find the total occurrence.

## Example 1: Python program to calculate relative frequency of a list of numbers

```# Program to Calculate Relative Frequency
# in Python of a list

def calcRelFreq(x):
relFreq = [(value, x.count(value) / len(x)) for value in set(x)]
return relFreq

dataValues = [1, 1, 1, 2, 3, 4, 4, 5, 2, 5]
print("The values of dataset are ", dataValues)

relFreq = calcRelFreq(dataValues)
print("The relative frequency of dataset is ", relFreq)
```

### Output

The output of the above program is:

```The values of dataset are  [1, 1, 1, 2, 3, 4, 4, 5, 2, 5]
The relative frequency of dataset is  [(1, 0.3), (2, 0.2), (3, 0.1), (4, 0.2), (5, 0.2)]```

## Calculation of Relative Frequency of a Column in DataFrame

In a similar way, we can calculate the relative frequency for values of a column in a dataFrame, we just need to pass the column in the function.

## Example 2: Python program to calculate the relative frequency of a DataFrame column

```# Program to calculate relative frequency in Python
# of a DataFrame Column
import pandas as pd

def calcRelFreq(x):
relFreq = [(value, x.count(value) / len(x)) for value in set(x)]
return relFreq

dataValues = pd.DataFrame(
{
"english": ["A", "B", "A", "A", "C", "B", "D"],
"hindi": ["B", "B", "C", "A", "A", "D", "D"],
}
)
print("The values of dataset are ", dataValues)

relFreq = calcRelFreq(list(dataValues["hindi"]))
print("The relative frequency of dataset is ", relFreq)
```

### Output

The output of the above program is:

```The values of dataset are    english hindi
0       A     B
1       B     B
2       A     C
3       A     A
4       C     A
5       B     D
6       D     D
The relative frequency of dataset is  [('D', 0.2857142857142857), ('C', 0.14285714285714285), ('A', 0.2857142857142857), ('B', 0.2857142857142857)]```