EDIT: This question is not a clone of pandas dataframe replace nan values with average of columns because I want to replace the value of each column with th
You can also use fillna
df = pd.DataFrame({'A': [1, 2, np.nan], 'B': [2, np.nan, np.nan]})
df.fillna(df.mean(axis=0))
A B
0 1.0 2.0
1 2.0 2.0
2 1.5 2.0
df.mean(axis=0)
computes the mean for every column, and this is passed to the fillna method.
This solution is on my machine, twice as fast as the solution using apply for the data set shown above.