Series.value_counts method in Pandas

The pandas.Series.value_counts() method counts the number of times that each unique value occurs in a Series. It is a powerful tool for exploratory data analysis, allowing us to quickly understand the distribution of values within our data.

Syntax a) Series.value_counts()
b) Series.value_counts(normalize=True)
If True, then the object returned will contain the relative frequencies of the unique values.

Example: Create a Series.

Python

data = [100, 200, 300, 500, 700, 300, 400, 700, 500, 600]
newseries = pd.Series(data)
print(newseries)

Above we have created a series and now we are going to find the unique values and its frequencies in the series.

Python

# Get value counts
print(newseries.value_counts())

The output of the about code is shown in the image below:

Series.value_counts method in Pandas

Explanation: • 300 appears 2 times.
• 500 appears 2 times.
• 100 appears only 1 time in the series, and so on.

Example: Using the normalize Parameter.

Python

# Get relative frequencies
newseries.value_counts(normalize=True) 

The output of the about code is shown in the image below:

Series.value_counts method in Pandas

Explanation: • 300 makes up 20% of the entries.
• 500 also makes up 20% of the entries.
• 100 makes up 10% of the entries, and so on.