Last Updated : 11 Jul, 2025
Python is popular for data analysis thanks to its powerful libraries and Pandas is one of the best. It makes working with data simple and efficient. The Index.value_counts() function in Pandas returns the count of each unique value in an Index, sorted in descending order so the most frequent item comes first. By default, it ignores any missing (NA) values. Example:
Python
import pandas as pd
import numpy as np
idx = pd.Index(['python', 'java', 'php'])
print(idx.value_counts())
python 1 java 1 php 1 Name: count, dtype: int64
Explanation:
Index.value_counts(normalize=False, sort=True, ascending=False, bins=None, dropna=True)
Parameters:
Parameter
Description
normalize
If True, returns relative frequencies (percentages) instead of raw counts.
sort
If True, sorts the result by count values.
ascending
If True, sorts counts in ascending order by default, it sorts in descending order.
bins
Groups numeric values into intervals instead of exact counts, useful for range-based grouping.
dropna
If True (default), ignores NaN values, set to False to include them.
Returns: A Pandas Series containing the counts (or frequencies) of unique values.
Examples of Index.value_counts()Examples 1: In this example, we demonstrate the Index.value_counts() method's handling of duplicate data and the normalize parameter that converts raw counts into proportions (percentages).
Python
import pandas as pd
import numpy as np
a = pd.Index(['Python', 'Java', 'Python'])
print(a.value_counts())
print(a.value_counts(normalize=True))
Python 2 Java 1 Name: count, dtype: int64 Python 0.666667 Java 0.333333 Name: proportion, dtype: float64
Explanation:
Example 2: In this example, we demonstrate the Index.value_counts() method's handling of duplicate data with sort=False to retain the original order and ascending=True to sort the counts in ascending order.
Python
import pandas as pd
import numpy as np
a = pd.Index(['Python', 'Java', 'Python'])
print(a.value_counts(sort=False))
print(a.value_counts(ascending=True))
Python 2 Java 1 Name: count, dtype: int64 Java 1 Python 2 Name: count, dtype: int64
Explanation:
Example 3: In this example, we demonstrate the Index.value_counts() method's handling of NaN values with the dropna=False parameter and its ability to group numeric values into bins using the bins=2 parameter.
Python
import pandas as pd
import numpy as np
a = pd.Index(['apple', np.nan, 'banana', np.nan])
print(a.value_counts(dropna=False))
b = pd.Index([5, 15, 25, 10])
print(b.value_counts(bins=2))
NaN 2 apple 1 banana 1 Name: count, dtype: int64 (4.979, 15.0] 3 (15.0, 25.0] 1 Name: count, dtype: int64
Explanation:
Related Articles:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4