3

I have a dataframe of the form:

enter image description here

For same values of col1 and col2 (for example, A B), I want to add all values in the col3 of the dataframe such that only one row of that form (A B) remains and all values in col3 correspond to those are added.

The result would look like:

enter image description here

I tried:

df.groupby(['col1', 'col2'], axis=0, as_index=True).sum()

but it gave me:

enter image description here

which is not exactly what I am looking for. Please help and advise. Thanks in advance.

PyAndy
  • 83
  • 1
  • 2
  • 8
  • Please include minimum reproducible code, so we can copy-paste your DF in our environment and help you solve this problem – Arsik36 Aug 14 '20 at 19:59
  • Please add code and data as text ([using code formatting](//stackoverflow.com/editing-help#code)), not images. Images: A) don't allow us to copy-&-paste the code/errors/data for testing; B) don't permit searching based on the code/error/data contents; and [many more reasons](//meta.stackoverflow.com/a/285557). Images should only be used, in addition to text in code format, if having the image adds something significant that is not conveyed by just the text code/error/data. – Patrick Artner Aug 14 '20 at 20:00
  • `.reset_index()`: https://stackoverflow.com/questions/21767900/how-to-move-pandas-data-from-index-to-column-after-multiple-groupby – ALollz Aug 14 '20 at 20:01
  • Thanks for the comments guys, will keep in mind :) – PyAndy Aug 14 '20 at 20:12

1 Answers1

6

You just need to do this, False instead of True:

df.groupby(['col1', 'col2'], axis=0, as_index=False).sum()
NYC Coder
  • 7,424
  • 2
  • 11
  • 24
  • 2
    `import pandas as pd`and `df = pd.DataFrame ( { "col1":list("AAABBBBABCDA"), "col2":list("ABABABABCDDA"), "col3":list(range(10,130,10))})` would make it more complete – Patrick Artner Aug 14 '20 at 20:22