How to use `bins` parameter?

The bins parameter in the value_counts() function is used to bin continuous data into discrete intervals. This is particularly useful when you want to categorize numerical data into ranges.

Syntax

Series.value_counts(bins=number_of_bins)

Example

Here's how to use the bins parameter:

import pandas as pd

# Creating a sample Series with numerical data
data = pd.Series([1, 2, 2, 3, 3, 3, 4, 4, 5, 6, 7, 8, 9, 10])

# Using value_counts with bins
counts = data.value_counts(bins=5)

print(counts)

Output

(0.999, 2.8]     3
(2.8, 4.6]      4
(4.6, 6.4]      3
(6.4, 8.2]      2
(8.2, 10.0]     2
dtype: int64

In this example, the numerical values in the Series are binned into 5 intervals. The output shows the count of values that fall within each bin range.

0 Comments

no data
Be the first to share your comment!