如何使用 Julia 数据框按函数进行滚动分组

问题描述 投票:0回答:1

问题-

如何在 julia 中通过滚动函数运行一个简单的组?我很难通过此代码行:

rolling_mean = rolling(rollmean, group_data[!, value_col], window_size)



using DataFrames
using RollingFunctions

# Create an example DataFrame
data = DataFrame(group = repeat(["A", "B"], outer = 5),
                 time = 1:10,
                 value = rand(10) * 100)

# Display the DataFrame
println("Original DataFrame:")
println(data)

# Function to calculate rolling mean for each group
function rolling_group_mean(df::DataFrame, group_col::Symbol, value_col::Symbol, window_size::Int)
    # Create a new DataFrame for results
    result = DataFrame()

    # Loop through each unique group
    for g in unique(df[!, group_col])
        group_data = df[df[!, group_col] .== g, :]

        # Calculate rolling mean and convert to a vector
        rolling_mean = rolling(rollmean, group_data[!, value_col], window_size)

        # Create a DataFrame with the results
        group_result = DataFrame(group = fill(g, length(rolling_mean)),
                                  time = group_data.time,
                                  rolling_mean = rolling_mean)

        # Append results to the result DataFrame
        append!(result, group_result)
    end

    return result
end

# Apply the function
window_size = 3
result_df = rolling_group_mean(data, :group, :value, window_size)

# Display the result
println("Rolling Mean DataFrame:")
println(result_df)

我收到以下错误:

ERROR: LoadError: BoundsError: attempt to access 0-element Vector{Any} at index [1]
Stacktrace:
 [1] getindex
   @ ./essentials.jl:13 [inlined]
 [2] rts
   @ ~/.julia/packages/RollingFunctions/fR9uW/src/support/utils.jl:49 [inlined]
 [3] basic_rolling(window_fn::typeof(rollmean), data1::Vector{Float64}, window_span::Int64)
   @ RollingFunctions ~/.julia/packages/RollingFunctions/fR9uW/src/roll/rollvectors.jl:17
 [4] #rolling#7
   @ ~/.julia/packages/RollingFunctions/fR9uW/src/roll/roll.jl:4 [inlined]
 [5] rolling(window_fn::typeof(rollmean), data1::Vector{Float64}, window_span::Int64)
   @ RollingFunctions ~/.julia/packages/RollingFunctions/fR9uW/src/roll/roll.jl:1
 [6] rolling_group_mean(df::DataFrame, group_col::Symbol, value_col::Symbol, window_size::Int64)
   @ Main 
julia
1个回答
0
投票

使用“Padding”定义滚动均值函数。
由于应用具有窗口大小的滚动平均值会减少元素数量,因此我定义了一些辅助函数来填充每个组滚动平均值的开头,例如: “缺失”值。
这应该确保滚动平均值与原始数据帧正确对齐。

function rolling_mean_padded(x, window_size)
    mean_vals = rollmean(x, window_size)
    padding_length = window_size - 1
    return vcat(fill(missing, padding_length), mean_vals)
end

grouped_data = groupby(data, :group)
window_size = 3

result_df= transform(grouped_data, :value => (x -> rolling_mean_padded(x, window_size)) => :rolling_mean) 
println(resutl_df)
© www.soinside.com 2019 - 2024. All rights reserved.