Querying Distinct Count in Pandas Using 'nunique()'
To perform a count of distinct values in Pandas, similar to the 'count(distinct)' function in SQL, use the 'nunique()' method.
The syntax for 'nunique()' is:
dataframe.groupby(groupby_column).column_to_count.nunique()
Example:
To count the distinct clients per year in a Pandas DataFrame named 'table':
table.groupby('YEARMONTH').CLIENTCODE.nunique()
Explanation:
Output:
The output of the query will be a series with the 'YEARMONTH' as the index and the count of distinct 'CLIENTCODE' values as the values.
YEARMONTH 201301 2 201302 3
The above is the detailed content of How to Query Distinct Count in Pandas Using \'nunique()\'?. For more information, please follow other related articles on the PHP Chinese website!