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.
While analyzing the data, many times the user wants to see the unique values in a particular column. Pandas nunique()
is used to get a count of unique values.
To download the CSV file used, Click Here.
Syntax: Series.nunique(dropna=True)
Parameters:
dropna: Exclude NULL value if TrueReturn Type: Integer – Number of unique values in a column.
Example #1: Using nunique()
In this example, nunique() method is used to get number of all unique values in Team column.
# importing pandas package import pandas as pd # making data frame from csv file data = pd.read_csv( "employees.csv" ) # storing unique value in a variable unique_value = data[ "Team" ].nunique() # printing value print (unique_value) |
Output:
The output of number of unique values is returned.
10
Example #2: NULL value Handling
In this example, length of array returned by unique() method is compared to integer returned by nunique() method.
# importing pandas package import pandas as pd # making data frame from csv file data = pd.read_csv( "employees.csv" ) # storing unique value in a variable arr = data[ "Team" ].unique() # storing unique value in a variable unique_value = data[ "Team" ].nunique(dropna = True ) # printing values print ( len (arr), unique_value) |
Output:
The output is not same in both of the cases as dropna parameter is set to True and hence NULL values were excluded while counting unique values.
11 10