Pandas Series nunique() Function:
Python nunique: The nunique() method of the Pandas module returns the number of unique items in a given Series.
Syntax:
Series.nunique(dropna=True)
Parameters
dropna: This is optional. To include NaN in the counts, set False. True is the default value.
Return Value:
The number of unique elements present in the given Series is returned by the nunique() function of the Pandas Series
- Python Pandas Series ge() Function
- Python Pandas Series ne() Function
- Python Pandas Series eq() Function
Python Pandas Series nunique() Function
Example1
Approach:
- Import pandas module using the import keyword.
- Pass some random list as an argument to the Series() function of the pandas module to create a series.
- Store it in a variable.
- Print the above-given series.
- Apply nunique() function on the given series to get the count of number of unique elements present in the given series and print the result.
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword. import pandas as pd # Pass some random list as an argument to the Series() function # of the pandas module to create a series. # Store it in a variable. gvn_series = pd.Series([3, 4, 1, 5, 4, 1, 3]) # Print the above given series print("The given series is:") print(gvn_series) print() # Apply nunique() function on the given series to get the # count of number of unique elements present in the given series # and print the result. print("The count of number of unique elements in the given series:") print(gvn_series.nunique())
Output:
The given series is: 0 3 1 4 2 1 3 5 4 4 5 1 6 3 dtype: int64 The count of number of unique elements in the given series: 4
Example2
Pandas nunique: Here, the nunique() function is used on a specified series/columns of a DataFrame.
Approach:
- Import pandas module using the import keyword.
- Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
- Store it in a variable.
- Print the given dataframe.
- Get the count of the number of unique elements present in the student_marks column of the dataframe using the nunique() function and print the result.
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword. import pandas as pd # Pass some random key-value pair(dictionary), index list as arguments to the # DataFrame() function of the pandas module to create a dataframe # Store it in a variable. data_frme = pd.DataFrame({ "student_rollno": [1, 2, 3, 4, 5, 6], "student_marks": [90, 35, 25, 90, 25, 90]}, index= ["virat", "nick" , "jessy", "sindhu", "john", "mary"] ) # Print the given dataframe print("The given Dataframe:") print(data_frme) print() # Get the count of number of unique elements present in the student_marks # column of the dataframe using the nunique() function and print the result print("Number of unique elements present in student_marks column of the dataframe:") print(data_frme['student_marks'].nunique())
Output:
The given Dataframe: student_rollno student_marks virat 1 90 nick 2 35 jessy 3 25 sindhu 4 90 john 5 25 mary 6 90 Number of unique elements present in student_marks column of the dataframe: 3