Pandas DataFrame nunique() Method
Example 1: Use nunique() function to find the number of unique values over the column axis.
Python3
# 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 |
Output:
Let’s use the dataframe.nunique() function to find the unique values across the column axis.
Python3
# find unique values df.nunique(axis = 1 ) |
Output:
As we can see in the output, the function prints the total no. of unique values in each row.
Example 2: Use nunique() function to find the number of unique values over the index axis in a Dataframe. The Dataframe contains NaN values.
Python3
# 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 ) |
Output:
The function is treating the empty string as a unique value in column 2.
Pandas dataframe.nunique() Method
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Contact Us