I am looking for a way how to shift DataFrame column by more rows.
Shifting by one row works fine:
df = DataFrame(A=[1,2,3,4], B=[9,8,7,6])
julia> transform(df, "A" => ShiftedArrays.lag => :A1)
4×3 DataFrame
Row │ A B A1
│ Int64 Int64 Int64?
─────┼───────────────────────
1 │ 1 9 missing
2 │ 2 8 1
3 │ 3 7 2
4 │ 4 6 3
But I am not able to find out how to transform the entire column with a function with more arguments, something like this (neither works):
transform(df, "A" => x -> ShiftedArrays.lag(x, 2) => :A1)
or
transform(df, ["A", 2] => f => :A1)
I hope there is a more suitable solution than using of for loop :-)
CodePudding user response:
You need additional parentheses around the lambda function:
transform(df, "A" => (x -> ShiftedArrays.lag(x, 2)) => :A1)
Result:
Row │ A B A1
│ Int64 Int64 Int64?
─────┼───────────────────────
1 │ 1 9 missing
2 │ 2 8 missing
3 │ 3 7 1
4 │ 4 6 2