为了账号安全,请及时绑定邮箱和手机立即绑定

Pandas 将 groupby 中几列总和的最大值的列名称获取到新列

Pandas 将 groupby 中几列总和的最大值的列名称获取到新列

慕容708150 2024-01-16 15:09:24
我有一个这种形式的数据框:        value1  value2  value3  value4 random string column    groupindex1       10      2       3       4                stuff  group 2index2       5       4       3       2          other stuff  group 1index3       6       7       8       9          other stuff  group 1index4       1       2       2       4      yet other stuff  group 2index5       6       1       8      11          other stuff  group 1可以使用以下代码生成此测试示例:df = pd.DataFrame([[10, 2, 3, 4, 'stuff', 'group 2'], [5, 4, 3, 2, 'other stuff', 'group 1'], [6, 7, 8, 9, 'other stuff', 'group 1'], [1, 2, 2, 4, 'yet other stuff', 'group 2'], [6, 1, 8, 11, 'other stuff', 'group 1']], columns = ['value1', 'value2', 'value3', 'value4', 'random string column', 'group'], index=['index1', 'index2', 'index3', 'index4', 'index5'])我想根据此规范创建一个名为“组识别列”的新列:按组列分组取每个值列的总和获取每组总和最大的值列的列名在此示例中,预期输出为:        value1  value2  value3  value4 random string column    group Group Identifying Columnindex1      10       2       3       4                stuff  group 2                   value1index2       5       4       3       2          other stuff  group 1                   value4index3       6       7       8       9          other stuff  group 1                   value4index4       1       2       2       4      yet other stuff  group 2                   value1index5       6       1       8      11          other stuff  group 1                   value4我已经尝试了几次 groupby / apply / transform 等,但我不能完全让它正确。
查看完整描述

2 回答

?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

让我们按组提取列然后映射:


max_cols = (df.filter(like='value')       # choose the value columns, also df.iloc[:, :4]

              .groupby(df['group']).sum() # calculate sum per group

              .idxmax(axis=1)             # find col with max value

           )


df['Column'] = df['group'].map(max_cols)

还groupby().transform():


df['Column'] = (df.filter(like='value')

                  .groupby(df['group']).transform('sum')

                  .idxmax(axis=1)

               )

输出:


        value1  value2  value3  value4 random string column    group  Column

index1      10       2       3       4                stuff  group 2  value1

index2       5       4       3       2          other stuff  group 1  value4

index3       6       7       8       9          other stuff  group 1  value4

index4       1       2       2       4      yet other stuff  group 2  value1

index5       6       1       8      11          other stuff  group 1  value4


查看完整回答
反对 回复 2024-01-16
?
江户川乱折腾

TA贡献1851条经验 获得超5个赞

这是map和的一种潜在解决方案dictionary:


#creates a dictionary with the maximum sum per group

d = df.groupby('group').sum().idxmax(axis=1).to_dict()


#mapping the dictionary to 'group' column to generated a new column

df['Group Identifying Column'] = df['group'].map(d)

或者,您可以切断该dictionary部分并简单地执行以下操作:


df['Group Identifying Column'] =  df.group.map(df.groupby('group').sum().idxmax(axis=1))

输出:


        value1  value2  ...    group  Group Identifying Column

index1      10       2  ...  group 2                    value1

index2       5       4  ...  group 1                    value4

index3       6       7  ...  group 1                    value4

index4       1       2  ...  group 2                    value1

index5       6       1  ...  group 1                    value4


查看完整回答
反对 回复 2024-01-16
  • 2 回答
  • 0 关注
  • 40 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信