user1753640
user1753640

Reputation: 375

Pandas groupby sum changes the key, why?

I have this dataset called 'event'

id   event_type_1   event_type_2   event_type_3
234             0              1              0
234             1              0              0
345             0              0              0

and I want to produce this

id   event_type_1   event_type_2   event_type_3
234             1              1              0
345             0              0              0

I tried using

event.groupby('id').sum()

but that just produced

id   event_type_1   event_type_2   event_type_3
1               1              1              0
2               0              0              0

The id has has been replaced with an incremental value starting at '1'. Why? And how do I get my desired result?

Upvotes: 0

Views: 302

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210832

Use as_index=False parameter:

In [163]: event.groupby('id', as_index=False).sum()
Out[163]:
    id  event_type_1  event_type_2  event_type_3
0  234             1             1             0
1  345             0             0             0

From the docs:

as_index : boolean, default True

For aggregated output, return object with group labels as the index. Only relevant for DataFrame input. as_index=False is effectively “SQL-style” grouped output

Upvotes: 2

Related Questions