0

I want to do like this How do I create a new column from the output of pandas groupby().sum()?

as show in the high scored answer, df['Data4'] = f['Data3'].groupby(df['Date']).transform('sum')

however, I want to groupby two columns.

thanks

1 Answers1

0

You can do df['Data4'] = df.groupby(['Date', 'second_grouping_column'])['Data3'].transform('sum'). As you can see on this page, the groupby method can take a list of columns

drewseph
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 16 '23 at 21:00