Python | Pandas Series.nunique()
Last Updated :
17 Sep, 2018
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 True
Return 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.
import pandas as pd
data = pd.read_csv( "employees.csv" )
unique_value = data[ "Team" ].nunique()
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.
import pandas as pd
data = pd.read_csv( "employees.csv" )
arr = data[ "Team" ].unique()
unique_value = data[ "Team" ].nunique(dropna = True )
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...