I have a dataset will some missing data that looks like this:
id category value 1 A NaN 2 B NaN 3 A 10.5
You can also use GroupBy + transform to fill NaN values with groupwise means. This method avoids inefficient apply + lambda. For example:
GroupBy
transform
NaN
apply
lambda
df['value'] = df['value'].fillna(df.groupby('category')['value'].transform('mean')) df['value'] = df['value'].fillna(df['value'].mean())