Getting Number of Unique Values

tags: #python/data_science/eda

The nunique() Method

The nunique() function is a method in panda used to calculate the number of unique values in a pandas Series or DataFrame. This method returns the count of distinct or unique values in a specified axis.

Serires

# Count the number of unique values in the Series 
unique_count = data.nunique()

DataFrames

# Count the number of unique values in each column of the DataFrame 
unique_counts = df.nunique()

Using the axis Parameter

The method has a axis parameter that determines the axis along which the unique values are counted. The axis parameter can take two possible values:

When axis = 0:

# Count the number of unique values down the horizonal axis
unique_counts_axis0 = df.nunique(axis=0)

When axis = 1:

# Count the number of unique values across the vertical axis
unique_counts_axis1 = df.nunique(axis=1)
Powered by Forestry.md