My df
id var1
A 9
A 0
A 2
A 1
B 2
B 5
B 2
B 1
C 1
C 9
D 7
D 2
D 0
..
desired output will have a dummy =1 if only var1 contains non-zeros, =0 otherwise.
id var1
A 0
B 1
C 1
D 0
CodePudding user response:
There are many ways, one could be to clip
to only have 0/1 in the input:
df['var1'].clip(upper=1).groupby(df['id']).min()
# or
# df.groupby('id')['var1'].min().clip(upper=1)
Alternatively, use booleans:
df['var1'].ne(0).groupby(df['id']).all().astype(int)
output:
id
A 0
B 1
C 1
D 0
Name: var1, dtype: int64
for a DataFrame as output add .reset_index(name='var1')
:
id var1
0 A 0
1 B 1
2 C 1
3 D 0