Skip to content
Advertisement

Python pandas group non repeating values

Hi I have a data frame which looks like this

JavaScript

I would like to groupby and sum for non repeating values in col1 for e.g.

JavaScript

Is there any way I can do this via pandas functions?

Advertisement

Answer

IIUC, you could create groups using groupby + cumcount (where the nth occurrences of each col1 value will be grouped the same); then groupby the groups and join “col1″s and sum “col2″s:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement