idt_tt
idt_tt

Reputation: 601

pandas: groupby column result conditional on another column

i have a df which looks like this

   a  b
0  A  Y
1  A  N
2  A  N
3  B  N
4  B  N
5  B  N
6  B  N
7  C  N
8  C  Y

i would like to groupby by column 'a' and column 'b' should show 'Y' whenever at least one entry is 'Y' or 'N' if ALL entries are 'N'

   a  b
0  A  Y
1  B  N
2  C  Y

Upvotes: 2

Views: 61

Answers (1)

ScootCork
ScootCork

Reputation: 3676

You are looking for max.

df.groupby('a').max().reset_index()

   a  b
0  A  Y
1  B  N
2  C  Y

When doing string comparison 'Y' is greater than 'N' (i.e. 'Y' > 'N' == True), so taking the max of column b means it will be 'Y' if there is a single 'Y' in the group, otherwise 'N' will be the max value.

Upvotes: 3

Related Questions