groupby fillna

Solutions on MaxInterview for groupby fillna by the best coders in the world

showing results for - "groupby fillna"
Lianne
30 Jun 2017
1In [2400]: df
2Out[2400]:
3   A  B  C    D
40  1  1  1  1.0
51  1  1  1  NaN
62  1  1  1  3.0
73  3  3  3  5.0
8
9In [2401]: df['D'].fillna(df.groupby(['A','B','C'])['D'].transform('mean'))
10Out[2401]:
110    1.0
121    2.0
132    3.0
143    5.0
15Name: D, dtype: float64
16
17In [2402]: df['D'] = df['D'].fillna(df.groupby(['A','B','C'])['D'].transform('mean'))
18
19In [2403]: df
20Out[2403]:
21   A  B  C    D
220  1  1  1  1.0
231  1  1  1  2.0
242  1  1  1  3.0
253  3  3  3  5.0
Dottie
11 Aug 2020
1In [2396]: df.shape
2Out[2396]: (10000, 4)
3
4In [2398]: %timeit df['D'].fillna(df.groupby(['A','B','C'])['D'].transform('mean'))
5100 loops, best of 3: 3.44 ms per loop
6
7
8In [2397]: %timeit df.groupby(['A','B','C'])['D'].apply(lambda x: x.fillna(x.mean()))
9100 loops, best of 3: 5.34 ms per loop