我有一個DataFrame我想在@chain. 如何存盤中間結果?
using DataFrames, Chain
df = DataFrame(a = [1,1,2,2,2], b = [1,2,3,4,5])
5×2 DataFrame
Row │ a b
│ Int64 Int64
─────┼──────────────
1 │ 1 1
2 │ 1 2
3 │ 2 3
4 │ 2 4
5 │ 2 5
@chain df begin do stuff save 一些中間結果 do more stuff end
uj5u.com熱心網友回復:
這是一種解決方案:
using DataFrames, DataFramesMeta, Chain
df = DataFrame(a = [1,1,2,2,2], b = [1,2,3,4,5])
@chain df begin
@rsubset(:a == 1)
@aside global x = _.b
end
所以,這實際上真的很酷,并且提供了很多可能性。但是有沒有辦法讓這個在函式內部作業?如果您要global在函式中使用關鍵字,它將在全域環境中創建物件——這并不理想。
uj5u.com熱心網友回復:
您可以使用 將中間結果分配給變數,如果它在當前范圍內不存在,則@aside預先宣告它。local
julia> function asidedemo(df)
local y
newdf = @chain df begin
@rsubset(:a == 1)
# store the intermediate results
@aside y = _.b
# and continue with other stuff
@transform(:c = :a :b)
end
@show(y)
newdf
end
asidetest (generic function with 1 method)
julia> asidedemo(df)
y = [1, 2]
2×3 DataFrame
Row │ a b c
│ Int64 Int64 Int64
─────┼─────────────────────
1 │ 1 1 2
2 │ 1 2 3
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/433532.html
下一篇:從文本檔案創建字典
