0

I am taking python course and trying to find out how many different states in my data set.

The column is 'ST'

i have attached screenshot enter image description here

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
ants
  • 35
  • 1
  • 5
  • Does this answer your question? [Counting unique values in a column in pandas dataframe like in Qlik?](https://stackoverflow.com/questions/45759966/counting-unique-values-in-a-column-in-pandas-dataframe-like-in-qlik) – JonSG Feb 12 '22 at 15:24

1 Answers1

0

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
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • perfect thank you cory. I like the last one - i will use that – ants Feb 12 '22 at 15:30
  • Do you feel this is not a duplicate of https://stackoverflow.com/questions/45759966/counting-unique-values-in-a-column-in-pandas-dataframe-like-in-qlik? – JonSG Feb 12 '22 at 16:52