Change language

Python | Pandas Index.nunique ()

| |

Index.nunique() Pandas Index.nunique() returns the number of unique elements in an object. It returns a scalar value that is a count of all unique values ​​in the index. By default, NaN values ​​are ignored. If dropna is False then NaN is included in the counter.

Syntax: Index.nunique (dropna = True)

Parameters:
dropna: Don’t include NaN in the count.

Returns: nunique: int

Example # 1: Use Index.nunique () () to find the number of unique values ​​in the index. Do not include NaN values ​​in the number.

# import pandas as pd

import pandas as pd

 
# Create index

idx = pd.Index ([ ’Beagle’ , ’Pug’ , ’ Labrador’ , ’Pug’ ,

  ’Mastiff’ , None , ’Beagle’ ])

< code class = "undefined spaces">  
# Print index
idx

Output:

Let’s find the number of unique values ​​in the index.

# to find the number of unique values.

idx.nunique (dropna = True )

Output:

As we see in the output , the function returned 4, indicating that there are only 4 unique values ​​in the index.

Example # 2: Use Ind ex.nunique () find all unique values ​​in the index. Also include missing values, that is, NaN values ​​in the number.

# import pandas as pd

import pandas as pd

  
# Create index

idx = pd.Index ([ ’Beagle’ , ’Pug’ , ’ Labrador’ , ’Pug’ ,

  ’Mastiff’ , None , ’Beagle’ ])

 
# Print index
idx

Output:

Let’s find the number of unique values ​​in index.

# to find the number of unique values.

idx.nunique ( dropna = False )

Output:

As we can see in the output, the function returned 5, indicating that there are only 5 unique values ​​in the index. We’ve also included missing values ​​in the number.

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