If you want to count unique values in a pandas GroupBy object, you can use the nunique() function. This function returns the number of unique values within each group.
Here's a step-by-step guide:
import pandas as pd data = { 'Category': ['A', 'B', 'A', 'C', 'B', 'A', 'C', 'C'], 'Value': [10, 20, 10, 30, 20, 30, 40, 40] } df = pd.DataFrame(data) print(df) unique_counts = df.groupby('Category')['Value'].nunique() print(unique_counts) The output will show the number of unique 'Value' entries for each 'Category'.
nunique():unique_counts = df.groupby('Category')[['Value']].nunique() print(unique_counts) This will return a DataFrame with counts of unique values for each specified column within each group.
unique() function within an agg():unique_values = df.groupby('Category').agg({'Value': 'unique'}) print(unique_values) This will return a DataFrame with the unique values in the 'Value' column for each 'Category'.
where-clause random dart-async version-control amazon-cloudfront cockroachdb axes shorthand angular-http getattr