# How to find unique occurance of a value based an another value using pandas?

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.

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

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 )