Open In App

Pandas Index.value_counts()-Python

Last Updated : 01 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

[GFGTABS]
Python

import pandas as pd
import numpy as np

idx = pd.Index(['python', 'java', 'php'])
print(idx.value_counts())


[/GFGTABS]

Output

python    1
java      1
php       1
Name: count, dtype: int64

Explanation:

  • pd.Index() creates a Pandas Index object idx with values ‘python’, ‘java’, and ‘php’.
  • idx.value_counts() counts the occurrences of each unique value in the Index, returning 1 for each as all values appear once.

Syntax of Index.value_counts()

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).

[GFGTABS]
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))


[/GFGTABS]

Output

Python    2
Java      1
Name: count, dtype: int64
Python    0.666667
Java      0.333333
Name: proportion, dtype: float64

Explanation:

  • pd.Index([‘Python’, ‘Java’, ‘Python’]) creates a Pandas Index with ‘Python’ repeated twice and ‘Java’ once.
  • a.value_counts() returns counts with ‘Python’ = 2 and ‘Java’ = 1.
  • a.value_counts(normalize=True) shows proportions, with ‘Python’ at 66.67% and ‘Java’ at 33.33% of the total.

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.

[GFGTABS]
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))


[/GFGTABS]

Output

Python    2
Java      1
Name: count, dtype: int64
Java      1
Python    2
Name: count, dtype: int64

Explanation:

  • a.value_counts(sort=False) keeps original order ‘Python’ = 2, ‘Java’ = 1.
  • a.value_counts(ascending=True) sorts counts ascending ‘Java’ = 1, ‘Python’ = 2.

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.

[GFGTABS]
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))


[/GFGTABS]

Output

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:

  • a.value_counts(dropna=False) counts each unique value in the Index, including missing values (NaN).
  • b.value_counts(bins=2) splits numeric values into 2 equal-sized bins and counts how many fall into each range.

Related Articles:



Next Article

Similar Reads