简体   繁体   中英

How do I count only unique values with groupby using pandas/python?

What can I do to this pandas dataframe to get it to count only the unique/distinct values of "Unique_Id"? Everything I have tried gives me unique values of community instead, or throws an error.

df.groupby("Community")["Unique_Id"].count().sort_values(ascending = False)

This is the output I get:

Comunidad_Autónoma
Cataluña                534415
Comunidad Valenciana    475411
Madrid                  415047
Islas Canarias          171939
País Vasco              168297
Navarra                  57045
La Rioja                 26057
Name: Unique_Id, dtype: int64

One possible option is to use pandas.DataFrame.drop_duplicates before you call the groupby method. In the example below, Madrid has a duplicate Id:

import pandas as pd

df = pd.DataFrame(dict(
    Community = 'Cataluña,Madrid,Cataluña,Madrid,Cataluña,Madrid'.split(','),
    Unique_Id = [1, 2, 3, 4, 5, 2],
))

df1 = df.drop_duplicates(
        ['Community','Unique_Id']
    ).groupby(
        'Community'
    )['Unique_Id'].count().sort_values(ascending = False)

print(df1)
print(f'\nTotal Unique_Ids Across All Communities: {sum(df1.values)}')

Example Code In Python Tutor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM