TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Pandas Series.value_counts()

Pandas Series.value_counts() with What is Python Pandas, Reading Multiple Files, Null values, Multiple index, Application, Application Basics, Resampling, Plotting the data, Moving windows functions, Series, Read the file, Data operations, Filter Data etc.

<< Back to PANDAS

Pandas Series.value_counts()

The value_counts() function returns a Series that contain counts of unique values. It returns an object that will be in descending order so that its first element will be the most frequently-occurred element.

By default, it excludes NA values.

Syntax

Series.value_counts(normalize=False, sort=True, ascending=False, bins=None, dropna=True)

Parameters

  • normalize: If it is true, then the returned object will contain the relative frequencies of the unique values.
  • sort: It sort by the values.
  • ascending: It sort in the ascending order.
  • bins: Rather than counting the values, it groups them into the half-open bins that provide convenience for the pd.cut, which only works with numeric data.
  • dropna: It does not include counts of NaN.

Returns

It returns the counted series.

Example1

import pandas as pd
import numpy as np
index = pd.Index([2, 1, 1, np.nan, 3])
index.value_counts()

Output

1.0    2
3.0    1
2.0    1
dtype: int64

Example2

import pandas as pd
import numpy as np
index = pd.Index([2, 1, 1, np.nan, 3])
a = pd.Series([2, 1, 1, np.nan, 3])
a.value_counts(normalize=True)

Output

1.0    0.50
3.0    0.25
2.0    0.25
dtype: float64

Example3

import pandas as pd
index = pd.Index([1, 3, 2, 2, 1, np.nan])
index.value_counts()
a = pd.Series([1, 3, 2, 2, 1, np.nan])
a.value_counts(bins=2)

Output

(0.997, 2.0]    4
(2.0, 3.0]        1
dtype: int64

Example4

import pandas as pd
index = pd.Index([1, 3, 2, 2, 1, np.nan])
index.value_counts()
a = pd.Series([1, 3, 2, 2, 1, np.nan])
a.value_counts(dropna=False)

Output

2.0     2
1.0     2
NaN   1
3.0     1
dtype: int64

Next TopicPandas DataFrame




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf