我有一个有4列(A,B,C,D)的数据帧.D有一些NaN条目.我想用具有相同值A,B,C的D的平均值填充NaN值.
例如,如果A,B,C,D的值分别是x,y,z和Nan,那么我希望将NaN值替换为A的平均值,其中A,B,C的值为分别是x,y,z.
df['D'].fillna(df.groupby(['A','B','C'])['D'].transform('mean'))
会比 apply
In [2400]: df Out[2400]: A B C D 0 1 1 1 1.0 1 1 1 1 NaN 2 1 1 1 3.0 3 3 3 3 5.0 In [2401]: df['D'].fillna(df.groupby(['A','B','C'])['D'].transform('mean')) Out[2401]: 0 1.0 1 2.0 2 3.0 3 5.0 Name: D, dtype: float64 In [2402]: df['D'] = df['D'].fillna(df.groupby(['A','B','C'])['D'].transform('mean')) In [2403]: df Out[2403]: A B C D 0 1 1 1 1.0 1 1 1 1 2.0 2 1 1 1 3.0 3 3 3 3 5.0
细节
In [2396]: df.shape Out[2396]: (10000, 4) In [2398]: %timeit df['D'].fillna(df.groupby(['A','B','C'])['D'].transform('mean')) 100 loops, best of 3: 3.44 ms per loop In [2397]: %timeit df.groupby(['A','B','C'])['D'].apply(lambda x: x.fillna(x.mean())) 100 loops, best of 3: 5.34 ms per loop
我想你需要:
df.D = df.groupby(['A','B','C'])['D'].apply(lambda x: x.fillna(x.mean()))
样品:
df = pd.DataFrame({'A':[1,1,1,3], 'B':[1,1,1,3], 'C':[1,1,1,3], 'D':[1,np.nan,3,5]}) print (df) A B C D 0 1 1 1 1.0 1 1 1 1 NaN 2 1 1 1 3.0 3 3 3 3 5.0 df.D = df.groupby(['A','B','C'])['D'].apply(lambda x: x.fillna(x.mean())) print (df) A B C D 0 1 1 1 1.0 1 1 1 1 2.0 2 1 1 1 3.0 3 3 3 3 5.0