Python | Pandas Series.nunique()

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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


My Personal Notes arrow_drop_up

Developer in day, Designer at night GSoC 2019 with Python Software Foundation (EOS Design system)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.