You can use DataFrame.std
, which omit non numeric columns:
print (df.std())
S1 2.302173
S2 2.774887
S3 2.302173
dtype: float64
If need std
by columns:
print (df.std(axis=1))
0 3.785939
1 1.000000
2 3.000000
3 0.577350
4 3.055050
dtype: float64
If need select only some numeric columns, use subset:
print (df[['S1','S2']].std())
S1 2.302173
S2 2.774887
dtype: float64
There is different with numpy.std
by default parameter ddof
(Delta Degrees of Freedom):
- pandas by default
ddof=1
- numpy by default
ddof=0
So there are different outputs:
#ddof=1
print (df.std(axis=1))
0 3.785939
1 1.000000
2 3.000000
3 0.577350
4 3.055050
dtype: float64
#ddof=0
print (np.std(df, axis=1))
0 3.091206
1 0.816497
2 2.449490
3 0.471405
4 2.494438
dtype: float64
But you can change it very easy:
#same output as pandas function
print (np.std(df, ddof=1, axis=1))
0 3.785939
1 1.000000
2 3.000000
3 0.577350
4 3.055050
dtype: float64
#same output as numpy function
print (df.std(ddof=0, axis=1))
0 3.091206
1 0.816497
2 2.449490
3 0.471405
4 2.494438
dtype: float64
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…