Ruaridh George
Ruaridh George

Reputation: 43

In python, how can I replace a specific value in a data frame with its column mean?

I have found questions about replacing values in a column, but I don't know how to specifically replace each value with its column mean. For example, in the code provided, how would I replace each -1 with the mean of the column the -1 was found in? I'm pretty new to python and don't know where to go

df = pd.DataFrame({"A" : [1, 2, 4, -1, 6, 7, 8],
"B" : [2, 4, -1, 8, 10, 6, 7], 
"C" : [20, -1, 4, 8, 19, 1, 7] })

Upvotes: 2

Views: 64

Answers (1)

jezrael
jezrael

Reputation: 862601

Use DataFrame.mask with compare by DataFrame.eq for == for set mean:

df = df.mask(df.eq(-1), df.mean(), axis=1)
print (df)
          A          B          C
0  1.000000   2.000000  20.000000
1  2.000000   4.000000   8.285714
2  4.000000   5.142857   4.000000
3  3.857143   8.000000   8.000000
4  6.000000  10.000000  19.000000
5  7.000000   6.000000   1.000000
6  8.000000   7.000000   7.000000

Detail:

print (df.mean())
A    3.857143
B    5.142857
C    8.285714
dtype: float64

Upvotes: 3

Related Questions