【问题标题】:best way to use groupby or aggregate in pandas在熊猫中使用 groupby 或聚合的最佳方法
【发布时间】:2019-09-18 10:51:59
【问题描述】:

我有一个名为 clients 的表,我想根据用户 ID 显示某人注册或购买商品的次数。

目标是有一个表格,显示基于用户 ID 的registration_complete 和购买的总和

这是我写的代码。不幸的是,并非所有列都显示出来

  new_file= new_data.groupby(['userid']) 
  ['Registration_Complete','Purchase'].agg('sum')
  new_file.head(5)

这是我用来根据userid计算注册和购买的表格

 Event_day  timestamp        install  userid  registration   purchase
 1/1/1900   1/1/1900 16:10    yes     555221     1               0
 1/1/1900   1/1/1900 16:12    yes     555221     1               1
 2/19/2010  1/19/2010 16:40   no      533211     0               1
 2/19/2010  1/19/2016 16:53   yes     533211     0               1
 2/20/2017  2/20/2017 15:46   yes     53200      1               0
 3/15/2017  3/15/2018 15:48   yes     53200      1               0
 3/15/2017  3/15/2018 20:14   yes     53200      1               0

我想要一些能给我总和的东西

Event_day  timestamp        install  userid  registration   purchase
1/1/1900   1/1/1900 16:10    yes     555221     2               0
2/19/2010  1/19/2016 16:53   yes     533211     0               2
3/15/2017  3/15/2018 20:14   yes     53200      5               0

【问题讨论】:

    标签: pandas jupyter-notebook pandas-groupby concat pandasql


    【解决方案1】:

    IIUC 你可以保留其他列的firstlast 值,将字典传递给agg

    agg = {'Event_day': 'last', 'timestamp': 'last', 'install': 'last', 'registration': 'sum', 'purchase': 'sum'}
    df.groupby('userid').agg(agg).reset_index()
    
        userid  Event_day   timestamp       install registration    purchase
    0   53200   3/15/2017   3/15/2018 20:14 yes     3               0
    1   533211  2/19/2010   1/19/2016 16:53 yes     0               2
    2   555221  1/1/1900    1/1/1900 16:12  yes     2               1
    

    编辑:

    记住几个答案可能是正确的,我发现在它们之间进行性能测试很有趣

    时间

    dfg1 = df.groupby("userid")["install", "timestamp", "Event_day"].max()
    dfg2 = df.groupby("userid").sum()
    pd.concat([dfg1, dfg2], axis=1)
    

    每个循环 38.5 ms ± 393 µs(平均值 ± 标准偏差,7 次运行,每次 10 个循环)

    first_other_columns = df[['Event_day', 'timestamp', 'install',  'userid']].drop_duplicates(['userid'], keep='first')
    grouped = df.groupby(['userid']).sum().reset_index()
    pd.merge(grouped, first_other_columns, on=['userid'])
    

    每个循环 11.3 ms ± 100 µs(7 次运行的平均值 ± 标准偏差,每次 100 个循环)

    agg = {'Event_day': 'last', 'timestamp': 'last', 'install': 'last', 'registration': 'sum', 'purchase': 'sum'}
    df.groupby('userid').agg(agg).reset_index()
    

    每个循环 6.85 ms ± 62.5 µs(7 次运行的平均值 ± 标准偏差,每次 100 个循环)

    【讨论】:

    • 谢谢特里。使用这个查询可以让我得到需要的结果。
    【解决方案2】:

    您可以使用以下内容:

    import pandas as pd
    
    first_other_columns = new_file[['Event_day', 'timestamp', 'install',  'userid']].drop_duplicates(['userid'], keep='first')
    grouped = new_file.groupby(['userid']).sum().reset_index()
    grouped = pd.merge(grouped, first_other_columns, on=['userid'])
    

    这将允许您保留第一个时间戳,event_day 并安装和分组用户 ID。

    告诉我!我希望它有所帮助。 BR

    【讨论】:

    • 感谢 RenauV 的反馈。这很有帮助。
    【解决方案3】:

    您希望其他数据列发生什么情况?通过获取其他列的最大值,这样的事情似乎接近你想要的。

    dfg1 = df.groupby("userid")["Event_day", "timestamp", "install"].max()
    dfg2 = df.groupby("userid").sum()
    pd.concat([dfg1, dfg2], axis=1)
    

    输出

            Event_day timestamp install  registration  purchase
    userid                                                     
    53200   3/15/2018     20:14     yes             3         0
    533211  1/19/2016     16:53     yes             0         2
    555221   1/1/1900     16:12     yes             2         1
    
    
    
    

    【讨论】:

    • Thnaks Colbster,这个查询步骤很有见地。
    猜你喜欢
    • 2023-02-25
    • 1970-01-01
    • 2021-03-21
    • 2015-12-10
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    • 1970-01-01
    • 2017-01-11
    相关资源
    最近更新 更多