df = pd.DataFrame([[1, 1], [1, -1], [2, 1], [2, 2]], columns=list('ab'))
print(df)
data = df.groupby(['a'])

def replace(group):
    mask = group < 0
group[mask] = group[~mask].mean()
return group
print(data.transform(replace))
Замена значений на groupby означает