xxxxxxxxxx
words = ['a', 'b', 'c', 'a']
unique_words = set(words) # == set(['a', 'b', 'c'])
unique_word_count = len(unique_words) # == 3
xxxxxxxxxx
#TO count repetition of each unique values(to find How many times the same-
# unique value is appearing in the data)
item_counts = df["Your_Column"].value_counts()
#Returns Dictionary => {"Value_name" : number_of_appearences}
xxxxxxxxxx
pd.value_counts(df.Account_Type)
Gold 3
Platinum 1
Name: Account_Type, dtype: int64
xxxxxxxxxx
# Basic syntax:
len(set(my_list))
# By definition, sets only contain unique elements, so when the list
# is converted to a set all duplicates are removed.
# Example usage:
my_list = ['so', 'so', 'so', 'many', 'duplicated', 'words']
len(set(my_list))
--> 4
# Note, list(set(my_list)) is a useful way to return a list containing
# only the unique elements in my_list
xxxxxxxxxx
import pandas as pd
# Assuming the DataFrame is already defined and contains the desired column
df = pd.DataFrame({'column_name': ['value1', 'value2', 'value1', 'value3', 'value2']})
# Count the number of unique values in the 'column_name' column
unique_count = df['column_name'].nunique()
# Print the result
print("Number of unique values:", unique_count)
xxxxxxxxxx
from collections import Counter
words = ['a', 'b', 'c', 'a']
Counter(words).keys() # equals to list(set(words))
Counter(words).values() # counts the elements' frequency