Query relating to Pandas Rows manipulation
Thank you for the above answer!
That definitely works. However, I found a more efficient way in terms of computation using np.rolling
df['D'] = df['A'].rolling(min_periods=1, window=3).mean()
df['B'] = np.where(df['B'].isnull,df['D'],df['B'])

np.rolling helps to compute the cumulative sum of previous n values.
np.where helps to apply some output based on a condition: syntax: np.where(condition,
value if true, value if false).
Column D can be dropped once it is used.