gpt4 book ai didi

dataframe - julia 数据框 - 如何按组获取下一行的值

转载 作者:行者123 更新时间:2023-12-04 07:57:03 24 4
gpt4 key购买 nike

在 Julia DataFrame 中,如何进行分组并使用下一行的值?
例如:

using DataFrames, DataFramesMeta
df = DataFrame(grp=["one", "one", "two", "two", "three"], val=[1, 2, 3, 4, 5])
# Row │ grp val
# │ String Int64
#─────┼───────────────
# 1 │ one 1
# 2 │ one 2
# 3 │ two 3
# 4 │ two 4
# 5 │ three 5

@combine(groupby(df, :grp),
count = length(:val),
first_val = first(:val),
#next_val = next(:val)
)
#3×3 DataFrame
# Row │ grp count first_val
# │ String Int64 Int64
#─────┼──────────────────────────
# 1 │ one 2 1
# 2 │ two 2 3
# 3 │ three 1 5

# I would like to obtain:

# Row │ grp count first_val next_val
# │ String Int64 Int64
#─────┼──────────────────────────
# 1 │ one 2 1 2
# 2 │ two 2 3 4
# 3 │ three 1 5 NA

最佳答案

使用 Julia DataFrames.jl 它将是例如:

julia> combine(groupby(df, :grp),
nrow => :count,
:val => first => :first_val,
:val => (x -> length(x) > 1 ? x[2] : missing) => :next_val)
3×4 DataFrame
Row │ grp count first_val next_val
│ String Int64 Int64 Int64?
─────┼────────────────────────────────────
1 │ one 2 1 2
2 │ two 2 3 4
3 │ three 1 5 missing
如果你接受额外的包,那么使用 ShiftedArrays.jl 它将是:
julia> using ShiftedArrays

julia> combine(groupby(df, :grp),
nrow => :count,
:val => first => :first_val,
:val => first∘lead => :next_val)
3×4 DataFrame
Row │ grp count first_val next_val
│ String Int64 Int64 Int64?
─────┼────────────────────────────────────
1 │ one 2 1 2
2 │ two 2 3 4
3 │ three 1 5 missing
这是相同的,但使用自动生成的列名:
julia> combine(groupby(df, :grp), nrow, :val => first, :val => first∘lead)
3×4 DataFrame
Row │ grp nrow val_first val_first_lead
│ String Int64 Int64 Int64?
─────┼──────────────────────────────────────────
1 │ one 2 1 2
2 │ two 2 3 4
3 │ three 1 5 missing

关于dataframe - julia 数据框 - 如何按组获取下一行的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66650628/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com