Mick
Mick

Reputation: 245

How to find the length of value_counts() that is greater than 1 in a pandas dataframe column

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

Answers (1)

Erfan
Erfan

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

Related Questions