9951 explained code solutions for 126 technologies


python-pandasHow 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().sizectrl + c
import pandas as pd

load 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