Python | Pandas dataframe.nunique ()



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.