Change language

Python | Pandas Series.value_counts ()

|

Pandas Series — it is a one-dimensional array with axis labels. Tags do not have to be unique, but must be hashable. The object supports indexing based on integers and labels and provides many methods for performing index-related operations.

Series.value_counts() Pandas Series.value_counts () returns a series containing the number of unique values. The resulting object will be in descending order, so the first item is the most common item. Excludes NA values ​​by default.

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

Parameter:
normalize: If True then the object returned will contain the relative frequencies of the unique values.
sort: Sort by values.
ascending: Sort in ascending order.
bins: Rather than count values, group them into half-open bins, a convenience for pd.cut, only works with numeric data.
dropna: Don’t include counts of NaN.

Returns : counts: Series

Example # 1: Use Series.value_counts () to find the unique values ​​of each item in this Series object.

# import t pandas as pd

import pandas as pd

 
# Create series

sr = pd.Series ([ ’New York’ , ’Chicago’ , ’ Toronto’ , ’Lisbon’ , ’ Rio’ , ’Chicago’ , ’Lisbon’ ])

  
# Print series

print (sr)

Output:

We will now use Series .value_counts () to find the number of values ​​for each unique value in a given Series object.

# find the value of the counters
sr.value_counts ()

Output:

As we see in the output , Series.value_counts () returned the counts of each unique value in this Series object.

Example # 2: Use Series.value_counts () to find the unique count values ​​of each item in a given Series object.

# import pandas as pd

import pandas as pd

 
# Create series

sr = pd.Series ([ 100 , 214 , 325 , 88 , None , 325 , None , 325 , 100 ])

 
# Print series

print (sr)

Output:

We will now use Series.value_counts () to find the number of values ​​for each unique value in a given Series object.

# find the value of the counters
sr.value_counts ()

Output:

As we can see in the output, Series.value_counts () returned value counts for each unique value in this Series object.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically