Count Unique Values by Group in pandas DataFrame

Learn how to count distinct values in a DataFrame column using pandas' nunique() in Python. ๐Ÿ“Š More details: https://statisticsglobe.com/c...

Count Unique Values by Group in pandas DataFrame
Statistics Globe
815 views โ€ข Oct 19, 2022
Count Unique Values by Group in pandas DataFrame

About this video

How to count the distinct values by group in the column of a pandas DataFrame in the Python programming language. More details: https://statisticsglobe.com/count-unique-values-group-column-pandas-dataframe-python
Python code of this video:

import pandas as pd # Import pandas

data = pd.DataFrame({'values':[1, 2, 3, 1, 1, 1, 1, 2, 2], # Create example DataFrame
'groups':['a', 'a', 'a', 'a', 'b', 'b', 'c', 'c', 'c']})
print(data) # Print example DataFrame

count_unique = data.groupby('groups')['values'].nunique() # Apply unique function
print(count_unique) # Print count of unique values
# groups
# a 3
# b 1
# c 2
# Name: values, dtype: int64

Follow me on Social Media:
Facebook โ€“ Statistics Globe Page: https://www.facebook.com/statisticsglobecom/
Facebook โ€“ R Programming Group for Discussions & Questions: https://www.facebook.com/groups/statisticsglobe
Facebook โ€“ Python Programming Group for Discussions & Questions: https://www.facebook.com/groups/statisticsglobepython
LinkedIn โ€“ Statistics Globe Page: https://www.linkedin.com/company/statisticsglobe/
LinkedIn โ€“ R Programming Group for Discussions & Questions: https://www.linkedin.com/groups/12555223/
LinkedIn โ€“ Python Programming Group for Discussions & Questions: https://www.linkedin.com/groups/12673534/
Twitter: https://twitter.com/JoachimSchork

Music by bensound.com

Tags and Topics

Browse our collection to discover more content in these categories.

Video Information

Views

815

Likes

14

Duration

3:09

Published

Oct 19, 2022

Related Trending Topics

LIVE TRENDS

Related trending topics. Click any trend to explore more videos.