Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python Pandas - Return the relative frequency from Index object
To return the relative frequency from an Index object, use the index.value_counts() method with the normalize parameter set to True. This calculates the proportion of each unique value relative to the total count.
Syntax
index.value_counts(normalize=True)
Creating a Pandas Index
First, let's create a Pandas Index with some duplicate values ?
import pandas as pd
# Creating Pandas index with duplicate values
index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30])
print("Pandas Index...")
print(index)
print(f"\nNumber of elements: {index.size}")
print(f"Data type: {index.dtype}")
Pandas Index... Index([50, 10, 70, 110, 90, 50, 110, 90, 30], dtype='int64') Number of elements: 9 Data type: int64
Getting Relative Frequency
Use value_counts(normalize=True) to get the relative frequency of each unique value ?
import pandas as pd
index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30])
# Get relative frequency (proportions)
relative_freq = index.value_counts(normalize=True)
print("Relative frequency:")
print(relative_freq)
# Compare with absolute counts
absolute_counts = index.value_counts(normalize=False)
print("\nAbsolute counts:")
print(absolute_counts)
Relative frequency: 50 0.222222 110 0.222222 90 0.222222 10 0.111111 70 0.111111 30 0.111111 Name: count, dtype: float64 Absolute counts: 50 2 110 2 90 2 10 1 70 1 30 1 Name: count, dtype: int64
How It Works
The relative frequency is calculated by dividing each count by the total number of elements. In our example:
- Values 50, 110, 90 appear 2 times each: 2/9 ? 0.222222
- Values 10, 70, 30 appear 1 time each: 1/9 ? 0.111111
- All frequencies sum to 1.0 (100%)
Conclusion
Use value_counts(normalize=True) to get relative frequencies as proportions instead of raw counts. This is useful for understanding the distribution of values in your Index as percentages of the total.
Advertisements
