Home > Back-end >  How to merge groupby with original dataframe ??
How to merge groupby with original dataframe ??

Time:05-17

I have a dataframe with 32 columns, something like that :

    company_id       Price           Date     others columns
       10           258.33        2021-08-01    ...
       11           300.00        2022-01-01    ...
       12           760.82        2021-11-01    ...

I want a groupby by company and by month :

df.groupby(['company_id', 'Date']).sum()

This works perfectly fine but how to merge the output with my initial dataframe above ? Like for all the rows. There will be doublons but it's fine.

CodePudding user response:

IUUC, what you want is groupby and transform

out = (df.groupby(['company_id', 'Date'])
       .transform(sum)
       .rename(columns=lambda col: f'{col}_sum'))
out = df.join(out)
print(out)

   company_id   Price        Date  Price_sum
0          10  258.33  2021-08-01     258.33
1          11  300.00  2022-01-01     300.00
2          12  760.82  2021-11-01     760.82
  • Related