0

I have a dataframe with duplicates, and I would like for a given column to sum the values of the duplicates and for another column calculate the mean of the duplicates. The picture below illustrates what I want to achieve.

enter image description here

I would like something this

df1.groupby(["A", "B"], as_index=False){"C": sum ,"D": mean}

I checked this solution Apply multiple functions to multiple groupby columns but the output structure is not what I want.

Any help would be appreciated.

Babas
  • 377
  • 3
  • 16
  • You're missing an `agg()` method after `groupby`. Also, community can address your question better if you could update your question with a sample data. – ashkangh Sep 07 '21 at 14:58
  • `df1.groupby(["A", "B"], as_index=False).agg({"C": "sum", "D": "mean"})` Missing agg, missing quotations around aggregate functions – Henry Ecker Sep 07 '21 at 15:07

1 Answers1

1

As @ashkangh recommended, you could use the agg() function. Something like this df.duplicated().groupby().agg(mean, sum).