0

My dataframe has four columns: date, source, campaign and spend. Now I have duplicated value in date, source and campaign, and I wanted to sum the spend if date, source and campaign (together) is the same. So each day there will be only one source, campaign and spend

enter image description here

and my code is:

marketing_spend_dict_df['spend_update'] = 
marketing_spend_dict_df.groupby(['date','source','campaign'])['spend'].sum()

I get an error saying "incompatible index of inserted column with frame index" How could I deal with it? I tried to search on Google but didn't find an optimal solution Thanks!

Stidgeon
  • 2,662
  • 8
  • 19
  • 28

1 Answers1

0

The issue could be due to multiindex. Checkout this link Attach a calculated column to an existing dataframe. I believe some SO user can mark it as duplicate with higher rep.

new_column = marketing_spend_dict_df.groupby(['date','source','campaign'], as_index=False)['spend'].sum()
marketing_spend_dict_df["spend_update"] = new_column.reset_index(level=0, drop=True)
PraveenB
  • 1,042
  • 8
  • 11