Muntasir
0
Q:

how to count number of unique values in a column python

df['hID'].nunique()
5
2
df = df.groupby('domain')['ID'].nunique()

print (df)
domain
'facebook.com'    1
'google.com'      1
'twitter.com'     2
'vk.com'          3
Name: ID, dtype: int64
0
pd.value_counts(df.Account_Type)

Gold        3
Platinum    1
Name: Account_Type, dtype: int64
1
df.nunique()
1
dataframe.column.nunique()
0
len(set(["word1", "word1", "word2", "word3"]))
# set is like a list but it removes duplicates
# len counts the number of things inside the set
0
df['var_1'].nunique()   # How many unque values are present in a variable
0
words = ['a', 'b', 'c', 'a']
unique_words = set(words)             # == set(['a', 'b', 'c'])
unique_word_count = len(unique_words) # == 3
0
aa="XXYYYSBAA"
bb=dict(zip(list(aa),[list(aa).count(i) for i in list(aa)]))
print(bb)
# output:
# {'X': 2, 'Y': 3, 'S': 1, 'B': 1, 'A': 2}
0

New to Communities?

Join the community