0

I think I have the same question as the one asked here How to do arithmetics on values of a column according to unique pair values of two other column but for Python.

I have three columns in my dataframe.

Id1 Id2 Points

what i want to finally get is a dataframe that contains only the those rows that have unique Id1 and Id2 pairs. In case there is redundancy in the original dataframe, I would like to have the value of 'Points' in my new dataframe as the sum of values from the original dataframe that correspond to that unique (Id1, Id2) pair.

Within Pandas, I have looked at 'groupby' (followed by a '.sum()') and 'sort_values' but I could not think of a way to pull off what I wanted to.

Here is what I want to do-

Originial

Id1 Id2 Points

1 3 18

2 6 15

1 3 17

1 4 11

1 1 15

New

Id1 Id2 Points

1 1 15

1 3 35

1 4 11

2 6 15

Please excuse the poor formatting

AkaiShuichi
  • 144
  • 1
  • 1
  • 9
  • Can you add some data sample, 5 rows [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) with expected output? – jezrael Nov 10 '19 at 05:50
  • Then use `df = df.groupby(['Id1','Id2'], as_index=False)[' Points'].sum()` – jezrael Nov 10 '19 at 06:00
  • 1
    @jezrael That worked like a charm. Thanks! – AkaiShuichi Nov 10 '19 at 06:09
  • Think you can use pivot_table for this 'df2 = df.pivot_table(values='Points', index=['Id1', 'Id2'], aggfunc='sum') df2.sort_values('Points')' – sre Nov 10 '19 at 12:02

0 Answers0