I have a df for which I want to patch the negative values (replace them with mean of the group).
I'm confused as to why the following throws an error:
df['patched_vals']=df.groupby(['grps'])['vals'].transform(lambda x: x if (x >0) else x.mean())While this works fine:
df['patched_vals']=df.groupby('grps').vals.transform(lambda x: x +(x.mean()-x)*(x<0))