I need to assign a value to all registers assigned as 0 (zero) in a column ('A'). This new value will be the mean value of every registers that share the same value registered on another column ('B'), i.e.: all rows that have 'A' assigned as 0 will have their value replaced by the mean of 'A' found among those who share the same value on 'B'. Apparently, the following code is not working, because, when I call print(df.A)
after it, I have some rows with 'A' as 0 returned:
df = df[df.A == 0].groupby('B')['A'].mean().reset_index()
I tried a bunch of line codes, but some aren't even accepted...
What I expect is a situation that all 0 values for A are replaced for a mean of A column grouped by B column. Like this:
Before:
Output:
A B
1 0 7
2 0 7
3 9 7
4 10 6
5 8 6
6 0 6
7 0 2
After:
Output:
A B
1 3 7
2 3 7
3 9 7
4 10 6
5 8 6
6 3 6
7 0 2
Thank you for your support.