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 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
Recommended Posts:
- Python | pandas.map()
- Python | Pandas Series.agg()
- Python | Pandas dataframe.mean()
- Python | Pandas dataframe.max()
- Python | Pandas Series.dt.tz
- Python | Pandas.CategoricalDtype()
- Python | Pandas dataframe.div()
- Python | Pandas Index.min()
- Python | Pandas Index.max()
- Python | Pandas.Categorical()
- Python | Pandas Index.where
- Python | Pandas dataframe.get()
- Python | Pandas Index.all()
- Python | Pandas Index.any()
- Python | Pandas dataframe.mad()
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.