I am taking python course and trying to find out how many different states in my data set.
The column is 'ST'
I am taking python course and trying to find out how many different states in my data set.
The column is 'ST'
You can use pandas.unique
to collect the unique elements of a series
>>> import pandas as pd
>>> df = pd.DataFrame(data={'ST': ['IL', 'WI',' LA', 'UT', 'IL']})
>>> pd.unique(df['ST'])
array(['IL', 'WI', ' LA', 'UT'], dtype=object)
and then use the size
of the series
>>> pd.unique(df['ST']).size
4
similarly you can just call the nunique
method off the series itself
>>> df['ST'].nunique()
4