0

What is the quickest way to keep the original instances after they get aggregated with pandas.groupby?

For example, given a dataframe df

  entries column  values
0  entry1      A     1.5 
1  entry2      A     1.3
2  entry3      A     0.6
3  entry4      B     0.7
4  entry5      B     0.5
5  entry6      C     0.5
6  entry7      C     1.3
7  entry8      C     1.7

and running groupby with mean as aggregation, I would like to obtain something like the following

  entries column    mean
0  entry1      A     1.333
1  entry2      A     1.333
2  entry3      A     1.333
3  entry4      B     0.6
4  entry5      B     0.6
5  entry6      C     1.666
6  entry7      C     1.666
7  entry8      C     1.666

i.e. the repeated property for all the original instances.

James Arten
  • 523
  • 5
  • 16
  • 2
    As per the suggested duplicate, you are looking for `transform`. E.g. something like: `df = df.assign(mean=df.groupby('column')['values'].transform('mean')).drop('values', axis=1)` – ouroboros1 Feb 09 '23 at 22:59
  • 1
    Thanks, that's what I was looking for indeed. I didn't know about `.transform()`! – James Arten Feb 11 '23 at 10:40

0 Answers0