Skip to content

DOC: DataFrame on df.nunique causing confusion #41180

Closed
@regmibijay

Description

@regmibijay

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.nunique.html

Documentation problem

The above documentation provides an example using pd.DataFrame({'A': [1, 2, 3], 'B': [1, 1, 1]}) upon which df.nunique() returns

A    3
B    1

This example might be a bit misleading as there are 3 and 1 present in dataframe as column elements and cause confusion.

Suggested fix for documentation

I would like to suggest using something like pd.DataFrame({'A': [5, 6, 7], 'B': [5, 1, 1]}) upon which df.nunique() returns

A 3
B 2

as this examples serves the purpose of nunique better as its clear that it counts the number of unique elements in column and does not return anything resembling any column element.

Furthermore, I would like to extend the example dataframe to df.nunique(axis=1) and match corresponding values with new dataframe as :

df.nunique(axis=1)
0    1
1    2
2    2
dtype: int64

Does this change need to be documented in whatsnew?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions