簡體   English   中英

計算每個pandas.DataFrame列的numpy.std?

[英]Calculate numpy.std of each pandas.DataFrame's column?

我想得到我的numpy.std的每一列的pandas.DataFrame

這是我的代碼:

import pandas as pd
import numpy as np

prices = pd.DataFrame([[-0.33333333, -0.25343423, -0.1666666667],
                       [+0.23432323, +0.14285714, -0.0769230769],
                       [+0.42857143, +0.07692308, +0.1818181818]])

print(pd.DataFrame(prices.std(axis=0)))

這是我的代碼輸出:

pd.DataFrame([[ 0.39590933],
              [ 0.21234018],
              [ 0.1809432 ]])

這是正確的輸出(如果使用np.std計算)

pd.DataFrame([[ 0.32325862],
              [ 0.17337503],
              [ 0.1477395 ]])

為什么我有這樣的差異? 我該如何解決這個問題?

注意 :我試過這樣做:

print(np.std(prices, axis=0))

但我有以下錯誤:

Traceback (most recent call last):
  File "C:\Users\*****\Documents\******\******\****.py", line 10, in <module>
    print(np.std(prices, axis=0))
  File "C:\Python33\lib\site-packages\numpy\core\fromnumeric.py", line 2812, in std
    return std(axis=axis, dtype=dtype, out=out, ddof=ddof)
TypeError: std() got an unexpected keyword argument 'dtype'

謝謝!

他們都是對的:他們只是默認的delta自由度是不同的。 np.std使用0, DataFrame.std使用1:

>>> prices.std(axis=0, ddof=0)
0    0.323259
1    0.173375
2    0.147740
dtype: float64
>>> prices.std(axis=0, ddof=1)
0    0.395909
1    0.212340
2    0.180943
dtype: float64
>>> np.std(prices.values, axis=0, ddof=0)
array([ 0.32325862,  0.17337503,  0.1477395 ])
>>> np.std(prices.values, axis=0, ddof=1)
array([ 0.39590933,  0.21234018,  0.1809432 ])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM