Skip to content

Instantly share code, notes, and snippets.

@dlwhitehurst
Last active March 31, 2020 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlwhitehurst/1aa24a0a4474cfa38a57e6abd69bfecf to your computer and use it in GitHub Desktop.
Save dlwhitehurst/1aa24a0a4474cfa38a57e6abd69bfecf to your computer and use it in GitHub Desktop.
Sum Grouped Column Values after Sort

Overview

Sometimes we need to combine values and de-dupe specific columns. Here's an example:

       A      B      C
1     foo    12    California
2     foo    22    California
3     bar    8     Rhode Island
4     bar    32    Rhode Island
5     baz    15    Ohio
6     baz    26    Ohio

You might want this:

      A       B      C
1    foo     34    California
2    bar     40    Rhode Island
3    baz     41    Ohio

Two solutions:

df.groupby(['A','C'])['B'].sum()

And ... this "returns" a DataFrame

df.groupby(['A','C'], as_index=False)['B'].sum()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment