Change language

pandas DataFrame nunique

pandas DataFrame.nunique function

DataFrame.nunique (axis=0, dropna=True)[source] Counts number of distinct elements in specified axis. Returns Series with number of distinct elements. Can ignore NaN values.

Name Description Type/Default Value Required / Optional
axis The axis to use. 0 or ‘index’ for row-wise, 1 or ‘columns’ for column-wise. {0 or ‘index’, 1 or ‘columns’}
Default Value: 0
Required
dropna Don’t include NaN in the counts. bool
Default Value: True
Required
Python is a great language for data analysis, mainly because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and it makes importing and analyzing data a lot easier. The Pandas dataframe.nunique () function returns the series with the number of distinct observations on the requested axis. If we set the axis value to 0, it finds the total number of unique observations on the index axis. If we set the axis value to 1, it finds the total number of unique observations on the column axis. It also provides the functionality to exclude NaN values ​​from the unique number count.

pandas DataFrame nunique Example #1


def get_nunique(self, colname):
        """
        Looks up or caches the number of unique (distinct) values in a column,
        or calculates and caches it.
        """
        return self.get_cached_value(nunique, colname, self.calc_nunique) 

pandas DataFrame nunique Example #2


def get_database_nunique(self, tablename, colname):
        colname = self.quoted(colname)
        sql = (’SELECT COUNT(DISTINCT %s) FROM %s WHERE %s IS NOT NULL’
               % (colname, tablename, colname))
        return self.execute_scalar(sql) 

pandas DataFrame nunique Example #3

Use nunique() function to find the number of unique values over the column axis.

# importing pandas as pd
import pandas as pd
  
# Creating the first dataframe 
df = pd.DataFrame({"A":[14, 4, 5, 4, 1],
                   "B":[5, 2, 54, 3, 2],
                   "C":[20, 20, 7, 3, 8],
                    "D":[14, 3, 6, 2, 6]})
  
# Print the dataframe
df

pandas DataFrame nunique Example #4

Use nunique() function to find the number of unique values over the index axis in a dataframe. The dataframe contains NaN values

# importing pandas as pd
import pandas as pd

# Creating the first dataframe
df = pd.DataFrame({"A":["Sandy", "alex", "brook", "kelly", np.nan],
				"B":[np.nan, "olivia", "olivia", "", "amanda"],
				"C":[20 + 5j, 20 + 5j, 7, None, 8],
				"D":[14.8, 3, None, 6, 6]})

# apply the nunique() function
df.nunique(axis = 0, dropna = True)

Archived version

The Pandas function dataframe.nunique() returns a series with the number of different observations along the requested axis. If we set the axis value to 0, then it will find the total number of unique observations along the index axis. If we set the axis value to 1, we get the total number of unique observations along the column axis. It also provides a function to exclude NaN values ​​from unique numbers.

Syntax: DataFrame.nunique (axis = 0, dropna = True)

Parameters:
axis: {0 or ’index’, 1 or ’columns’}, default 0
dropna: Don’t include NaN in the counts.

Returns: nunique: Series

Example # 1: Use nunique () to find the number of unique values ​​along the column axis.

# import pandas as pd

import pandas as pd

 
# Create first data frame

df = pd.DataFrame ( { "A" : [ 14 , 4 , 5 , 4 , 1 ],

"B" : [ 5 , 2 , 54 , 3 , 2 ],

"C" : [ 20 , 20 , 7 , 3 , 8 ],

"D" : [ 14 , 3 , 6 , 2 , 6 ]})

 
# Print the data frame
df

Let’s use the dataframe.nunique () function to find unique values ​​along the column axis.

# find unique values ​​

df.nunique (axis = 1 )

Output:

As we can see in the output, the function prints the total number. unique values ​​in each row.

Example # 2: Use nunique () to find the number of unique values ​​along the index axis in a data frame. The data frame contains NaN values.

# import pandas as pd

import pandas as pd

 
# Create first data frame

df = pd.DataFrame ({ "A" : [ " Sandy " , " alex " , "brook" , "kelly" , np.nan],

  " B " : [np.nan, "olivia" , "olivia" , " ", " amanda "], 

  " C " : [ 20 + 5j , 20 + 5j , 7 , None , 8 ],

"D" : [ 14.8 , 3 , None , 6 , 6 ]})

  
# apply nunique () function

df.nunique (axis = 0 , dropna = True )

Output:

The function treats an empty string as a unique value in column 2.

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

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

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 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

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

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