Issue
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
.
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
Answered By - Corralien
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.