selecting 'student_id' where 'pass' value is not 0 in every case

Viewed 25

I have a dataset. I want to select all rows where the value of the 'pass' is not all zero for a student_id

student_id      Subject      pass

234554          Maths        1
346535          Science      0  
862789          History      0
234554          English1     1
346535          History      0
862789          Sciencre     1

As for student_id 346535, there is not a single pass value as 1, therefore, we ignored it

Expected Output:

student_id      Subject      pass

234554          Maths        1
862789          History      0
234554          English1     1
862789          Sciencre     1
2 Answers

set_index + any

u = df.set_index('student_id')

u.loc[u.groupby(level=0)['pass'].any()]

             Subject  pass
student_id
234554         Maths     1
862789       History     0
234554      English1     1
862789      Sciencre     1

any works out of the box here with any because it is "falsey". If you are looking for a specific value, and not a "falsey" value, you can mask before you groupby to create the indexer for loc

u['pass'].ne(val).groupby(level=0).any()

you can set_index then create a mask by using cumsum and groupby

df.set_index('student_id',inplace=True) # set your index # 

mask = (df.loc[df.groupby(['student_id'])
             ['pass'].transform('cumsum') >= 1].index) # create your mask

df_new = df.loc[df.index.isin(mask)] # slice your df.

print(df_new.reset_index()) # print.


    student_id  Subject pass
0   234554  Maths   1
1   862789  History 0
2   234554  English1    1
3   862789  Sciencre    1
Related