1

I need to group elements and sum it with one column.

   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

And I want no unify member_id and event_path and sum event_duration. Desire output

       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

I use

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

but I get

   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

What I do wrong?

2 Answers 2

3

You need groupby with parameters sort=False and as_index=False with aggregation 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

Another possible solution is add 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

Function transform is used to add an aggregated calculation back to the original df as a new column.

Sign up to request clarification or add additional context in comments.

Comments

1

What you are doing wrong is that you try to assign it to a column in the original dataframe. And since your new column has less rows than the original dataframe, it gets repeated at the end.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.