Python ProgrammingPython Programming

Pandas Count Distinct Values of a DataFrame Column

Count Distinct Values:

import pandas as pd

df = pd.DataFrame({'Age': [30, 20, 22, 40, 20, 30, 20, 25],
                    'Height': [165, 70, 120, 80, 162, 72, 124, 81],
                    'Score': [4.6, 8.3, 9.0, 3.3, 4, 8, 9, 3],
                    'State': ['NY', 'TX', 'FL', 'AL', 'NY', 'TX', 'FL', 'AL']},
                   index=['Jane', 'Nick', 'Aaron', 'Penelope', 'Jaane', 'Nicky', 'Armour', 'Ponting'])

print(df.Age.value_counts())


C:\python\pandas>python example51.py
20    3
30    2
25    1
22    1
40    1
Name: Age, dtype: int64
 
C:\python\pandas>