Issue
I have the following df, and i want to create a dummy =1 if and only if each id does not contain any zeros in column "count".
id count
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
id count dummy
A 9 0
A 0 0
A 2 0
A 1 0
B 2 1
B 5 1
B 2 1
B 1 1
C 1 1
C 9 1
D 7 0
D 2 0
D 0 0
thanks
Solution
groupby().transform
is the way to go, but I'd groupby on the logic series itself
# transform `min` would work as well
df['dummy'] = df['count'].ne(0).groupby(df['id']).transform('all').astype(int)
Output:
id count dummy
0 A 9 0
1 A 0 0
2 A 2 0
3 A 1 0
4 B 2 1
5 B 5 1
6 B 2 1
7 B 1 1
8 C 1 1
9 C 9 1
10 D 7 0
11 D 2 0
12 D 0 0
Answered By - Quang Hoang
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.