1import pandas as pd
2from datetime import datetime
3
4def generate_data():
5 ...
6
7t = datetime.now()
8df = generate_data()
9df['value'] = df.groupby(['category', 'name'])['value']\
10 .transform(lambda x: x.fillna(x.mean()))
11print(datetime.now()-t)
12
13# 0:00:00.016012
14
15t = datetime.now()
16df = generate_data()
17df["value"] = df.groupby(['category', 'name'])\
18 .transform(lambda x: x.fillna(x.mean()))['value']
19print(datetime.now()-t)
20
21# 0:00:00.030022
1df['value'] = df.groupby(['category', 'name'])['value']\
2 .transform(lambda x: x.fillna(x.mean()))