Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cardinality / distinct count for all columns in pandas dataframe

Tags:

python

pandas

While the dataframe.describe() is useful for some summary descriptive statistics - specifically quartiles and range values - it apparently does not have a cardinality count option?

What are the options - or alternatively approaches - for obtaining cardinality counts in the dataframe - potentially by supplying a list of columns and defaulting to "all" ?

like image 845
WestCoastProjects Avatar asked Dec 07 '22 17:12

WestCoastProjects


2 Answers

You can count the distinct values of the dataframe series. This will give you the column name and the cardinality. For example for the dataframe:

names = pd.Categorical(['Tomba', 'Monica', 'Monica', 'Nancy', 'Neil', 'Chris'])
courses = pd.Categorical(['Physics', 'Geometry', 'Physics', 'Biology', 'Algebra', 'Algebra'])

df = pd.DataFrame({
    'Name' : names, 
    'Course': courses
})


Out[72]: df
     Course    Name
0   Physics   Tomba
1  Geometry  Monica
2   Physics  Monica
3   Biology   Nancy
4   Algebra    Neil
5   Algebra   Chris

df.apply(pd.Series.nunique)

Course    4
Name      5
dtype: int64
like image 187
Nancy Avatar answered Feb 06 '23 19:02

Nancy


Alternatively, you can use value_counts. Here is an example.

import pandas as pd

names = pd.Categorical(['Tomba', 'Monica', 'Monica', 'Nancy', 'Neil', 'Chris'])
courses = pd.Categorical(['Physics', 'Geometry', 'Physics', 'Biology', 'Algebra', 'Algebra'])
df = pd.DataFrame({'Name': names, 'Course': courses})

for col in df:
    cardinality = len(pd.Index(df[col]).value_counts())
    print(df[col].name + ": " + str(cardinality))
like image 36
Zhongjun 'Mark' Jin Avatar answered Feb 06 '23 17:02

Zhongjun 'Mark' Jin