【问题标题】:Pandas: Combine aggregated columns with not Aggregated columns in the same function callPandas:在同一个函数调用中将聚合列与非聚合列组合
【发布时间】:2021-04-05 19:13:37
【问题描述】:

我有一个 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:Little Derived Frame 创建关联 GroupBy1 - GroupName1 - GroupId1

lastTicketFrame:只有 LastTicketTime 的帧

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

measuresFrame:仅测量

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

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

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

【问题讨论】:

    标签: python pandas dataframe pandas-groupby


    【解决方案1】:

    您可以在单个agg 中执行此操作(通过按LastTicketTime 对值进行排序并在agg 中获取last):

    (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
    

    附:如果我没记错的话,您对 GroupName1 = First, GroupName2 = Second 的预期输出似乎有问题:MesCMesD 是总和,应该分别是 9 和 4(而不是 4 和 2 )。

    【讨论】:

    • 非常感谢,perl。与我所做的相比,这要容易得多
    猜你喜欢
    • 1970-01-01
    • 2020-03-06
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 2017-07-02
    相关资源
    最近更新 更多