Skip to content

API: add DataFrame.nunique() and DataFrameGroupBy.nunique() #14336

Closed
@xflr6

Description

@xflr6

When exploring a data set, I often need to df.apply(pd.Series.nunique) or df.apply(lambda x: x.nunique()). How about adding this as nunique()-method parallel to DataFrame.count() (count and unique are also the two most basic infos displayed by DataFrame.describe())?

I think there are also use cases for this as a groupby-method, for example when checking a candidate primary key for different lines (values):

>>> import pandas as pd
>>> df = pd.DataFrame({'id': ['spam', 'eggs', 'eggs', 'spam'], 'value': [1, 5, 5, 2]})
>>> df.groupby('id').filter(lambda g: (g.apply(pd.Series.nunique) > 1).any())
     id  value
0  spam      1
3  spam      2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions