search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
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
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

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. normalizelink | boolean | optional

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

2. sortlink | boolean | optional

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

3. ascendinglink | 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. binslink | 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. dropnalink | 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 2
5.0 1
4.0 1
dtype: 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.50
5.0 0.25
4.0 0.25
dtype: 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 2
5.0 1
4.0 1
dtype: int64

Setting sort=False will disable such sorting:

s = pd.Series([4,3,3,5,np.nan])
s.value_counts(sort=False)
4.0 1
3.0 2
5.0 1
dtype: 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 1
5.0 1
3.0 2
dtype: 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 2
5.0 1
4.0 1
dtype: 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] 1
dtype: 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 2
NaN 1
5.0 1
4.0 1
dtype: int64
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
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!