简体   繁体   中英

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

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' (ie '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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM