Reputation: 245
Is there a way to find the length of number of occurrences in a pandas dataframe column using value_counts()?
df['Fruits'].value_counts()
Apple 6
Orange 5
Pear 5
Peach 4
Watermelon 4
Strawberry 1
Honeydew 1
Cherry 1
when I try to run len(df['Fruits'].value_counts() != 1)
, my desired output would be:
5
However, it end up returning 8
which is the same value as len(df['Fruits'].value_counts())
Any idea why this is the case?
Upvotes: 1
Views: 3448
Reputation: 42906
You are looking for sum instead:
df['Fruits'].value_counts().ne(1).sum()
If you want to use len
, you have to filter first:
vals = df['Fruits'].value_counts()
len(vals[vals > 1])
Upvotes: 3