extract data with special value

Viewed 65

I have a dataset like:

df = pd.DataFrame({'scientist':["Wendelaar Bonga"," Sjoerd E.", "Grätzel"," Michael", "Willett", "Walter C.",
                             "Kessler", "Ronald C.", "Witten, Edward", "Wang, Zhong Lin"],
               'SubjectField': ["Biomedical Engineering", "Inorganic & Nuclear Chemistry",
                                "Organic Chemistry", "Biomedical Engineering", "Developmental Biology",
                                "Mechanical Engineering & Transports", "Biomedical Engineering", "Microbiology",
                                "Cardiovascular System & Hematology", "Biomedical Engineering"]})

I want to count the number of scientists in each subject field and extract subject fields that have more than 2 scientists. this is my code to count the number of scientists

 number_of_scientists_in_fields=data.groupby(['SubjectField'])['scientist'].count()

how can I extract subject fields that have more than 2 scientists?

3 Answers

Use value_counts, as follows:

fields = df.value_counts('SubjectField').to_frame('count')
res = fields[fields['count'] > 2]
print(res)

Output

                        count
SubjectField                 
Biomedical Engineering      4

Alternative approach, which maybe is not as good as Dani's could be this:

> df2 = df[df.SubjectField.duplicated(keep=False)]
> df2.groupby('SubjectField').count()
                        scientist
SubjectField
Biomedical Engineering          4

This example however will include 2 or more (not > 2)

You just need to create a Series and then filter it with > 2:

Use Groupby.count:

In [2554]: x = df.groupby('SubjectField')['scientist'].count()
In [2559]: ans = x[x > 2]

In [2560]: ans
Out[2560]: 
SubjectField
Biomedical Engineering    4
Name: scientist, dtype: int64
Related