I have a code that is roughly like this:
import numpy as np
import pandas as pd
df = pd.DataFrame({'Group':['a','a','b','b','b','c','c'], 'Label':[0,1,0,1,1,0,1], 'Num':[1,2,3,4,5,6,7]})
I would like to have a a new column that is the mean of Num, but only those with class label 1. However, this mean should be applied to all rows with label 1 only, with the rest being 0/NaN. The output should be like this:
mean = [0,2,0,4.5,4.5,0,7]
Also how would it be if instead of 0/Nan, you just apply that mean to all values of that group? i.e.
mean = [2,2,4.5,4.5,4.5,7,7]