Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Average of column B values per column A value

Goal: Append column Mean with the same average value propagated per column Model.

I want to append the mean of each Model‘s Time scores.

The Mean value should be the same, per Model.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df is already in this grouped order.

df:

Model | Time
-------------
bert | 0.0001432
bert | 0.0001123
mlp | 0.2321
mlp | 0.2324

Desired df:

Model | Time | Mean
-------------------
bert | 0.0001432 | 0.00012775
bert | 0.0001123 | 0.00012775
mlp | 0.2321 | 0.23225
mlp | 0.2324 | 0.23225

Code:

models = df.Model.unique()
mean = df.groupby('a')['b'].mean()
for m in models:
    sums[m]

df2 = df.assign(Average = mean)

Please let me know if there’s anything else I can add to post to clarify.

>Solution :

Use transform('mean') instead of mean() to broadcast values on each row:

df['Mean'] = df.groupby('Model').transform('mean')
print(df)

# Output
  Model      Time      Mean
0  bert  0.000143  0.000128
1  bert  0.000112  0.000128
2   mlp  0.232100  0.232250
3   mlp  0.232400  0.232250

Values are not rounded:

with pd.option_context('display.float_format', '{:,.10f}'.format):
    display(df)

# Output:
  Model         Time         Mean
0  bert 0.0001432000 0.0001277500
1  bert 0.0001123000 0.0001277500
2   mlp 0.2321000000 0.2322500000
3   mlp 0.2324000000 0.2322500000
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading