0

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!

Chris
  • 15,819
  • 3
  • 24
  • 37
user3064366
  • 1,509
  • 2
  • 11
  • 15
  • Does this answer your question? [Pandas: filling missing values by mean in each group](https://stackoverflow.com/questions/19966018/pandas-filling-missing-values-by-mean-in-each-group) – Chris Dec 09 '21 at 20:56

1 Answers1

0

This should solve your problem

df.replace(0, None).fillna(df.groupby('Class').transform('mean'))

Credit goes to @EdChurn in this answer