2

I was trying to calculate std for an array, i've tried to use numpy and pandas in order to find std, but what i achieved is not logical, i have two different std's for the same array !

Why does this happens ?

>>> import numpy as np
>>> import pandas as pd

>>> a = np.arange(10)+1
>>> a
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
>>> a.std()
2.8722813232690143
>>> b = pd.DataFrame(a)
>>> b.std()
0    3.02765
dtype: float64
Mohsen_Fatemi
  • 2,183
  • 2
  • 16
  • 25

1 Answers1

5

Difference is in degree of freedom, default in numpy is ddof=0, in pandas is ddof=1:

print(a.std())
2.8722813232690143
print(a.std(ddof=0))
2.8722813232690143

print(a.std(ddof=1))
3.0276503540974917

b = pd.DataFrame(a)
print(b.std())
0    3.02765
dtype: float64
print(b.std(ddof=1))
0    3.02765
dtype: float64

print(b.std(ddof=0))
0    2.872281
dtype: float64
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252