How to return the number of values that has a specific count

I would like to find how many occurrences of a specific value count a column contains. For example, based on the data frame below, I want to find how many values in the ID column are repeated twice

| ID       | 
| -------- | 
| 000001   |            
| 000001   | 
| 000002   | 
| 000002   | 
| 000002   | 
| 000003   | 
| 000003   | 

The output should look something like this

Number of ID's repeated twice: 2
The ID's that are repeated twice are: 

| ID       | 
| -------- | 
| 000001   | 
| 000003   | 

Any help would be appreciated.

Topic data-analysis pandas python

Category Data Science


You can use df['var'].value_counts() to get this info. Example:

import pandas as pd
x = pd.Series(['000001',
               '000001',
               '000002',
               '000002',
               '000002',
               '000003',
               '000003'])

vc = x.value_counts()
vc.index[vc == 2]
# Index(['000003', '000001'], dtype='object')

Beware though of potential conversion of the original data into strings for the series index though. (If that is a problem, using something like df.groupby('x',as_index=False).size() may be a better option.)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.