猿问

合并/连接包含重复 ID 和 TRUE/FALSE 值的行,仅保留 TRUE

给定一个数据框


{'id': {0: 100, 1: 100, 2: 101, 3: 101, 4: 102},

 'A': {0: True, 1: False, 2: True, 3: False, 4: True},

 'B': {0: False, 1: True, 2: False, 3: True, 4: False},

 'C': {0: False, 1: False, 2: False, 3: False, 4: False}}

看起来像这样

我们如何合并行,以便每个唯一 id 只有一行,并保留所有 True 值?

目标:

https://img.mukewang.com/64cb66b80001d0e803620230.jpg

用途groupby.max

df.groupby('id', as_index=False).max()


慕森卡
浏览 102回答 1
1回答

MM们

尝试用out = df.groupby('id').max().reset_index()自从True > Falsereturn : True
随时随地看视频慕课网APP

相关分类

Python
我要回答