将函数应用于数据框列

时间:2017-04-11 15:53:55

标签: python pandas dataframe

我有一个如下数据框。我想将我定义的函数(hurst())应用于每一列(它将时间序列作为参数),然后将结果输出到另一个数据帧。

def hurst(ts):
    #returns the Hurst Exponent of the time series vector ts

    # Create the range of lag values
    lags = range (1,5)

    # Calculate the array of the variances of the lagged differences
    tau = [sqrt(std(subtract(ts[lag:], ts[:-lag]))) for lag in lags]

    # Use a linear fit to estimate the Hurst Exponent
    poly = polyfit(log(lags), log(tau), 1)

    # Return the Hurst exponent from the polyfit output

    return poly[0]*2.0

我如何从中获取

enter image description here

enter image description here

完整数据框具有1y Dt历史记录并继续E,F等

1 个答案:

答案 0 :(得分:0)

试试吧:

def hurst(ts):
    #returns the Hurst Exponent of the time series vector ts

    # Create the range of lag values
    lags = range (1,5)

    # Calculate the array of the variances of the lagged differences
    tau = [sqrt(std(subtract(ts[lag:], ts[:-lag]))) for lag in lags]

    # Use a linear fit to estimate the Hurst Exponent
    poly = polyfit(log(lags), log(tau), 1)

    # Return the Hurst exponent from the polyfit output

    return poly[0]*2.0

df[list('ABCD')].apply(hurst)