Skip to content

Latest commit

 

History

History
38 lines (28 loc) · 819 Bytes

how-to-count-unique-values.md

File metadata and controls

38 lines (28 loc) · 819 Bytes

How to count unique values

import pandas as pd

df = pd.DataFrame({
  'type': ['A', 'A', 'A', 'B', 'B', 'C'],
  'age': [1, 3, 6, 3, 4, 1]
})

uniq_count = df.type.unique().size
  • import pandas as pd - load lib:Pandas module
  • pd.DataFrame - creates Pandas DataFrame object
  • df.type - address type column values
  • .unique() - will return unique values for a specified column
  • .size - return length of a given array
  • uniq_count - will contain unique values size

group: unique

Example:

import pandas as pd

df = pd.DataFrame({
  'type': ['A', 'A', 'A', 'B', 'B', 'C'],
  'age': [1, 3, 6, 3, 4, 1]
})

print(df.type.unique().size)
3