Absolute and Relative frequency in Pandas

Absolute and Relative frequency in Pandas

In Pandas, both absolute and relative frequencies can be calculated with ease.

  • Absolute Frequency: This is the count of occurrences of each unique value in a given series or dataframe. It's essentially a histogram for the data.
  • Relative Frequency: This is the proportion or fraction of the total occurrences for each unique value.

Let's demonstrate how to compute these frequencies with a simple example using a Pandas series:

import pandas as pd # Sample data data = ['cat', 'dog', 'cat', 'dog', 'bird', 'cat', 'bird', 'bird'] series = pd.Series(data) # Absolute Frequency absolute_frequency = series.value_counts() # Relative Frequency relative_frequency = series.value_counts(normalize=True) print("Absolute Frequency:") print(absolute_frequency) print("\nRelative Frequency:") print(relative_frequency) 

Output:

Absolute Frequency: cat 3 bird 3 dog 2 dtype: int64 Relative Frequency: cat 0.375 bird 0.375 dog 0.250 dtype: float64 

In the example, the value_counts() function is used to compute the absolute frequencies. By setting the normalize parameter to True, the relative frequencies can be computed.


More Tags

fastapi hessian memory-leaks typescript2.0 exif date-parsing checkstyle easymock innodb singleton

More Programming Guides

Other Guides

More Programming Examples