'pandas extract first row column value equal to 1 for each group

I have df:

date   id   label   pred
1/1     1     0      0.2
2/1     1     1      0.5
1/1     2     1      0.9
2/1     2     1      0.3

I want for each id, get the first row when label column equal to 1. for example desire df:

date    id   label   pred
2/1      1    1       0.3
1/1      2     1      0.9

thx!



Solution 1:[1]

First filter only rows with label=1 and then remove duplicates per id by DataFrame.drop_duplicates:

df1 = df[df['label'].eq(1)].drop_duplicates('id')

Solution 2:[2]

You can use groupby and take the first row after keep only rows where label is set to 1:

out = df[df['label'] == 1].groupby('id', as_index=False).first()
print(out)

# Output
   id date  label  pred
0   1  2/1      1   0.5
1   2  1/1      1   0.9

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jezrael
Solution 2 Corralien