In a pandas DataFrame, is it possible to collapse columns which have identical values, and sum up the values in another column?
Code
This is a job for groupby
:
>>> df.groupby(["score", "type"]).sum()
count
score type
9.397000 advanced 537.331573
9.397995 advanced 9.641728
9.397996 newbie 0.100000
9.399900 expert 19.6541374
>>> df.groupby(["score", "type"], as_index=False).sum()
score type count
0 9.397000 advanced 537.331573
1 9.397995 advanced 9.641728
2 9.397996 newbie 0.100000
3 9.399900 expert 19.654137