1
$\begingroup$

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.

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

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
| improve this answer | | | | |
$\endgroup$
  • $\begingroup$ Thanks. It works $\endgroup$ – Ravindra Prasad Jun 18 '19 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 '19 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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