df:
name group S1 S2 S3
A mn 1 2 8
B mn 4 3 5
C kl 5 8 2
D kl 6 5 5
E fh 7 1 3
output:
std (S1,S2,S3)
3.78
1
3
0.57
3.05
這是為了擷取列的std:
numpy.std(df['A'])
我想對行做同樣的事情
最佳答案 您可以使用
DataFrame.std,它省略非數字列:
print (df.std())
S1 2.302173
S2 2.774887
S3 2.302173
dtype: float64
如果需要std列:
print (df.std(axis=1))
0 3.785939
1 1.000000
2 3.000000
3 0.577350
4 3.055050
dtype: float64
如果需要隻選擇一些數字列,請使用子集:
print (df[['S1','S2']].std())
S1 2.302173
S2 2.774887
dtype: float64
預設情況下參數ddof(Delta Degrees of Freedom)與numpy.std有所不同:
> pandas預設為ddof = 1
> numpy預設為ddof = 0
是以有不同的輸出:
#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
但你可以很容易地改變它:
#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