Home > Blockchain >  Asign intermediate Results in DataFrame Chain
Asign intermediate Results in DataFrame Chain

Time:02-28

I have a DataFrame which I want to process with the help of a @chain. How can store intermediate results?

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 some intermediate result do more stuff end

CodePudding user response:

This is one solution:

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

So, this is actually really cool and gives a lot of possibilities. But is there any way to make this work inside a function? If you were to use the global keyword inside a function it would create the object in the global environment - which is not ideal.

CodePudding user response:

You can assign the intermediate results to a variable using @aside, pre-declaring it with local if it doesn't exist in the current scope already.

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
  • Related