Python ProgrammingPython Programming

Pandas Count distinct Values of one column depend on another column

Count distinct equivalent:

import pandas as pd

df = pd.DataFrame({'DateOfBirth': ['1986-11-11', '1999-05-12', '1976-01-01',
                                   '1986-06-01', '1983-06-04', '1990-03-07',
                                   '1999-07-09'],                   
                   'State': ['NY', 'TX', 'FL', 'AL', 'AK', 'TX', 'TX']
                   },
                  index=['Jane', 'Nick', 'Aaron', 'Penelope', 'Dean',
                         'Christina', 'Cornelia'])

print(df.groupby('State').DateOfBirth.nunique())




C:\pandas>python example60.py
State
AK    1
AL    1
FL    1
NY    1
TX    3
Name: DateOfBirth, dtype: int64
 
C:\pandas>