首页 > 解决方案 > Pandas:在同一个函数调用中将聚合列与非聚合列组合

问题描述

我有一个 DataFrame 有两列将在 group by( GroupBy1 和 GroupBy2 )中使用,有几十列将使用 agg() ( MesA:Max, MesB:Min, MesC:sum.. )和其他列进行测量这不是用于度量,而是用于 groupby 中最后一行的日期时间、'GroupName1'、'GroupName2'、另一个数据库的 GroupId 等信息。

TicketsDBFrame
GroupBy1 GroupBy2 GroupName1 GroupName2 MesA MesB MesC MesD LastTicketTime       GroupId1 GroupId2
1        1        First      First      2    3    1    6    2021-04-05 01:00:00  4        99
1        1        First      First      4    1    3    2    2021-04-05 02:00:00  4        99
1        1        First      First      2    5    2    1    2021-04-05 03:00:00  4        99
1        2        First      Second     2    5    2    1    2021-04-05 01:30:00  4        75
1        2        First      Second     1    4    7    3    2021-04-05 02:30:00  4        75
2        2        Second     Second     4    2    1    8    2021-04-05 02:00:00  2        75
2        2        Second     Second     1    6    3    1    2021-04-05 04:00:00  2        75

所需的输出:

GroupBy1 GroupBy2 GroupName1 GroupName2 MesA MesB MesC MesD LastTicketTime       GroupId1 GroupId2
1        1        First      First      4    1    6    9    2021-04-05 03:00:00  4        99
1        2        First      Second     2    4    4    2    2021-04-05 02:30:00  4        75
2        2        Second     Second     4    2    4    9    2021-04-05 04:00:00  2        75

我已经知道如何使用派生的DataFrames创建这个所需的Frame,使用'loc'和'idxmax'在一个框架中获取LastTicketTime,其他派生的框架到'Ids and Names'和另一个DataFrame来为度量列调用agg(),之后我在框架中进行合并

groupInfoFrame:创建关联 GroupBy1 - GroupName1 - GroupId1 的小派生框架

lastTicketFrame:只有 LastTicketTime 的帧

lastTicketFrame=TicketsDBFrame[['GroupBy1','GroupBy2','LastTicketTime' ]]
lastTicketFrame=lastTicketFrame.loc[lastTicketFrame.groupby(['GroupBy1'],['GroupBy2]).LastTicketTime.idxmax() ]

措施框架:仅措施

measuresFrame = TicketsDBFrame.groupby(['GroupBy1'],['GroupBy2]).agg( mesA:.....MesD )

毕竟我使用 GroupBy1 和 GroupBy2 作为键在 measureFrame 和 lastTicketFrame 中进行合并

是否可以在一个 agg() 或 transform() 或其他函数调用中包含所有这些信息?没有派生框架和合并

标签: pythonpandasdataframepandas-groupby

解决方案


您可以一次性完成agg(通过对值进行排序LastTicketTimelast输入agg):

(df
    .sort_values('LastTicketTime')
    .groupby(['GroupBy1', 'GroupBy2'], as_index=False)
    .agg({
        'GroupName1': 'last',
        'GroupName2': 'last',
        'MesA': 'max',
        'MesB': 'min',
        'MesC': 'sum',
        'MesD': 'sum',
        'LastTicketTime': 'last',
        'GroupId1': 'last',
        'GroupId2': 'last'
    }))

输出:

   GroupBy1  GroupBy2 GroupName1 GroupName2  MesA  MesB  MesC  MesD  \
0         1         1      First      First     4     1     6     9   
1         1         2      First     Second     2     4     9     4   
2         2         2     Second     Second     4     2     4     9   

        LastTicketTime  GroupId1  GroupId2  
0  2021-04-05 03:00:00         4        99  
1  2021-04-05 02:30:00         4        75  
2  2021-04-05 04:00:00         2        75

PS 如果我没记错的话,GroupName1 = First, GroupName2 = Second: 的预期输出中似乎存在问题:MesC并且MesD是总和,应该分别为 9 和 4(而不是 4 和 2)。


推荐阅读