Below is my dataset.

Student_code   Subject
___________    _______
123            Maths
123            Chemistry
124            Maths
125            Chemistry
126            Physics
126            Maths
127            Physics
127            Chemistry

I would like to find number of students who have just enrolled for one course. In this example, given "Maths", i should get 1 as answer as student id 124 enrolled just for Maths.

  • $\begingroup$ wouldn't student 125 also come up because of chemistry? $\endgroup$
    – MattR
    Jun 17, 2019 at 20:18
  • $\begingroup$ Thats right. Given Chemistry, i should get just 125. $\endgroup$ Jun 18, 2019 at 4:25

1 Answer 1


Assuming that you want to return students who only took one class, You can use groupby and filter:

df.groupby('Student_code', as_index=False).filter(lambda x: x['Subject'].count() ==1 )

  Student_code    Subject
3          124      Maths
4          125  Chemistry
  • $\begingroup$ Thanks. It works $\endgroup$ Jun 18, 2019 at 13:24
  • $\begingroup$ @RavindraPrasad, great! if this answered your question, please mark as "accepted" and/or upvote if this solved your problem. by marking accepted it lets the Data Science Network see that this question was answered $\endgroup$
    – MattR
    Jun 18, 2019 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.