【问题标题】:Pandas: sum values in some column熊猫:某列中的总和值
【发布时间】:2017-07-03 17:03:12
【问题描述】:

我需要对元素进行分组并将其与一列相加。

   member_id    event_path  event_duration
0        111        vk.com               1
1        111   twitter.com               4
2        111  facebook.com              56
3        111        vk.com              23
4        222      vesti.ru               6
5        222  facebook.com              23
6        222        vk.com              56
7        333      avito.ru               8
8        333       avito.ru               4
9        444       mail.ru               7
10       444        vk.com              20
11       444     yandex.ru              40
12       111     vk.com                 10
13       222     vk.com                 20

而且我不想统一 member_idevent_path 和总和 event_duration。 期望输出

       member_id    event_path  event_duration
0        111        vk.com              34
1        111   twitter.com               4
2        111  facebook.com              56
4        222      vesti.ru               6
5        222  facebook.com              23
6        222        vk.com              76
7        333      avito.ru               12
9        444       mail.ru               7
10       444        vk.com              20
11       444     yandex.ru              40

我用

df['event_duration'] = df.groupby(['member_id', 'event_path'])['event_duration'].transform('sum')

但我明白了

   member_id    event_path  event_duration
0        111        vk.com              34
1        111   twitter.com               4
2        111  facebook.com              56
3        111        vk.com              34
4        222      vesti.ru               6
5        222  facebook.com              23
6        222        vk.com              76
7        333      avito.ru              12
8        333      avito.ru              12
9        444       mail.ru               7
10       444        vk.com              20
11       444     yandex.ru              40
12       111        vk.com              34
13       222        vk.com              76

我做错了什么?

【问题讨论】:

    标签: python pandas group-by sum aggregate


    【解决方案1】:

    您需要 groupby 和参数 sort=Falseas_index=False 和聚合 sum

    df = df.groupby(['member_id','event_path'],sort=False,as_index=False)['event_duration'].sum()
    print (df)
       member_id    event_path  event_duration
    0        111        vk.com              34
    1        111   twitter.com               4
    2        111  facebook.com              56
    3        222      vesti.ru               6
    4        222  facebook.com              23
    5        222        vk.com              76
    6        333      avito.ru              12
    7        444       mail.ru               7
    8        444        vk.com              20
    9        444     yandex.ru              40
    

    另一种可能的解决方案是添加reset_index:

    df = df.groupby(['member_id', 'event_path'],sort=False)['event_duration'].sum().reset_index()
    print (df)
       member_id    event_path  event_duration
    0        111        vk.com              34
    1        111   twitter.com               4
    2        111  facebook.com              56
    3        222      vesti.ru               6
    4        222  facebook.com              23
    5        222        vk.com              76
    6        333      avito.ru              12
    7        444       mail.ru               7
    8        444        vk.com              20
    9        444     yandex.ru              40
    

    函数transform 用于将聚合计算作为新列添加回原始df。

    【讨论】:

      【解决方案2】:

      您做错了什么是您尝试将其分配给原始数据框中的列。而且由于您的新列的行数少于原始数据框,因此它会在最后重复。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-17
        • 1970-01-01
        • 2018-11-08
        • 2018-05-08
        • 2014-02-19
        • 1970-01-01
        相关资源
        最近更新 更多