Skip to content

Get the unique number of values in some column in Pandas

# this will give you the number of unique values in the "month_year" column
dta_small["month_year"].nunique()
# this gets the number of unique values for every column in the dataframe for each unique value in the "month_year" column
dta_small.groupby("month_year").nunique()
# this gets the number of unique values from the "author" column for each unique value found in the "month_year" column
dta_small.groupby("month_year")["author"].nunique()
# this gets count for each unique value in the"author" column and puts them into a dataframe for easy viewing result of the  print out are below
dta_filtered = dta.author.value_counts()

If you want the unique values as a list (or dataframe) then just replace the nunique with unique

Source

See also  Python code snippet - How to print float and string in single line ?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.