Home >Backend Development >Python Tutorial >How to Count Unique Values Grouped by a Column with Pandas?

How to Count Unique Values Grouped by a Column with Pandas?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-18 15:47:03861browse

How to Count Unique Values Grouped by a Column with Pandas?

Count Unique Values Per Groups with Pandas

Counting unique values grouped by a specific column is a common task in data analysis. Pandas provides various methods to achieve this.

In your case, you have a DataFrame with 'ID' and 'domain' columns and need to count unique 'ID' values for each 'domain'.

Using df.groupby['domain', 'ID'].count()':

This method returns a DataFrame with counts for both 'ID' and 'domain' groups. However, it counts the number of rows in each group, not just the unique 'ID' values.

Solution with `nunique()':

df.groupby('domain')['ID'].nunique() calculates the unique 'ID' count for each 'domain' group. The resulting DataFrame will have the 'domain' column as the index and the count as a new column.

Stripping Single Quotes:

If your 'domain' column contains single quotes, use df.domain.str.strip("'") to remove them before grouping.

Preserve Column Name:

To keep the 'ID' column name in the result, use df.groupby(by='domain', as_index=False).agg({'ID': pd.Series.nunique}). This will create a DataFrame with the 'domain' and 'ID' (unique count) columns.

The above is the detailed content of How to Count Unique Values Grouped by a Column with Pandas?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn