search
Search
Unlock 100+ guides
search toc
close
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
Doc Search
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Shrink
Navigate to
Pandas
655 guides
keyboard_arrow_down
check_circle
Mark as learned
thumb_up
0
thumb_down
0
chat_bubble_outline
0
Comment
auto_stories Bi-column layout
settings

# Pandas Series | value_counts method

schedule Aug 10, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Pandas `Series.value_counts(~)` method returns the count of unique values in the Series.

# Parameters

1. `normalize`link | `boolean` | `optional`

Whether or not to normalize the counts so that their sum is one. By default, `normalize=False`.

2. `sort`link | `boolean` | `optional`

Whether or not to sort by count. By default, `sort=True`.

3. `ascending`link | `boolean` | `optional`

This parameter is only relevant if `sort=True`.

• If `True`, then sort in ascending order

• If `False`, then sort in descending order

By default, `ascending=False`.

4. `bins`link | `int` | `optional`

If `bins` is set, then counts will be based on intervals. The width of the interval will be determined like so:

``` [max(Series) - min(Series)] / bins ```

By default, `bins=None`.

5. `dropna`link | `boolean` | `optional`

• If `True`, then `NaN` will be ignored.

• If `False`, then `NaN` will be counted as well.

By default, `dropna=True`,

# Return Value

A `Series`.

# Examples

## Basic usage

To get the count of the unique values in the Series:

``` s = pd.Series([4,3,3,5,np.nan])s.value_counts() 3.0 25.0 14.0 1dtype: int64 ```

Here, notice how the missing value was ignored since `dropna=True` by default.

## Specifying normalize

To normalize the counts so that they sum up to one:

``` s = pd.Series([4,3,3,5,np.nan])s.value_counts(normalize=True) 3.0 0.505.0 0.254.0 0.25dtype: float64 ```

## Specifying sort

By default, `sort=True`, which means that the resulting Series is sorted by the count:

``` s = pd.Series([4,3,3,5,np.nan])s.value_counts() 3.0 25.0 14.0 1dtype: int64 ```

Setting `sort=False` will disable such sorting:

``` s = pd.Series([4,3,3,5,np.nan])s.value_counts(sort=False) 4.0 13.0 25.0 1dtype: int64 ```

## Specifying ascending

By default, `ascending=True`, which means that the resulting Series is sorted by count in ascending order:

``` s = pd.Series([4,3,3,5,np.nan])s.value_counts() 4.0 15.0 13.0 2dtype: int64 ```

To sort by descending order instead, set `ascending=False` like so:

``` s = pd.Series([4,3,3,5,np.nan])s.value_counts(ascending=False) 3.0 25.0 14.0 1dtype: int64 ```

## Specifying bins

Instead of counting unique values, we can count based on an interval by passing `bins` like so:

``` s = pd.Series([4,3,3,5,np.nan])s.value_counts(bins=3) (2.9970000000000003, 3.667] 2(4.333, 5.0] 1(3.667, 4.333] 1dtype: int64 ```

Here, 2 values reside in the interval `2.997` and `3.667`.

Note that the width of each interval is computed by:

``` [max(s) - min(s)] / 3 = 2/3 = 0.666 ```

## Counting nan in value_counts

By default, `dropna=True`, which means that all `NaN` are ignored. We can choose to include them in the count by passing in `dropna=False` like so:

``` s = pd.Series([4,3,3,5,np.nan])s.value_counts(dropna=False) 3.0 2NaN 15.0 14.0 1dtype: int64 ```
Edited by 0 others
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!