2016-07-13 64 views

回答

5

您可以使用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 

有默認參數與numpy.std是不同ddof(自由三角洲度):默認情況下

  • 大熊貓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 
0

當你不能在行任何你可以列做你可以使用「轉」

np.std(df.transpose()['S1'])