Imputation by group

groupby groups a DataFrame's rows by key to aggregate, and transform computes one value per group while returning it with the same number of rows as the original: each row receives its group's result. It is used to add a share, group total, or rank column without losing rows.

Example

df['part'] = df['ventes'] / df.groupby('canal')['ventes'].transform('sum')

Related concepts

The whole glossary