Let, assume, I have a dataframe df as follow.
df = pd.DataFrame([(2, 3, 1),
                    (1, 4, 2),
                    (0, 6, 1),
                    (5,0, 2)],
                    columns=['Feature01', 'Feature02', 'Class'])
So, every 0 value of the feature should replace by the mean of the corresponding class. For instance, Feature 01 has four values [2, 1, 0, 5]. But, [2, 0] fall into the same class 1. So, mean in (2+0)/2 = 1. Therefore, 0 should be replaced by 1. Likewise, the result should be
Feature01   Feature02   Class
  2             3         1
  1             4         2
  1             6         1
  5             2         2
Is there any simple python coding to solve this without going to any long loop? Thank you!
 
     
    