![]() |
VOOZH | about |
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 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 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 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 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.
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: