Why mean is different in different way of calculation?

I calculate mean in two different way.But in the 1st way it did’t consider NaN as a item,but in 2nd way it consider NaN as a item and return a different mean.
Please you explain it.

First time, before you calculated actual Mean, you introduced df.loc['Mean']=0 before doing the calculation. Which means you have increased the number of entries by 1. So ‘Mean’ calcuated by create_mean_row is actually less than actual mean.

2nd time, when you calculate mean using np.mean, it will also consider ‘Mean’ as a row (inserted by create_mean_row).

Both are incorrect.

Better, drop the ‘Mean’ row. Calculate mean using np.mean and then create a new entry for ‘Mean’ and assign it.
Screenshot 2020-07-04 at 12.37.45 PM

1 Like

Thank you sir.
I understand .