F # deedle how to convert a frame to a list of tuples

Say I have the Framefollowing,

type Person = 
    { Name:string; Age:int; Comp1:float; Comp2:float }

let peopleRecds = 
    [ { Name = "Joe"; Age = 51; Comp1=12.1; Comp2 =20.3 }
      { Name = "Tomas"; Age = 28; Comp1=1.1; Comp2 =29.3 }
      { Name = "Eve"; Age = 2; Comp1=2.1; Comp2 =40.3 }
      { Name = "Suzanne"; Age = 15; Comp1=12.4; Comp2 =26.3} ]
let peopleList = Frame.ofRecords peopleRecds

How to convert peopleListto listfrom tuples?

+4
source share
1 answer

You can use FSharpValue.MakeTuple to create a tuple .

Then you need to specify the type or tuple:

open Microsoft.FSharp.Reflection

type Person =  { Name:string; Age:int; Comp1:float; Comp2:float }

let peopleRecds = 
    [ { Name = "Joe"; Age = 51; Comp1=12.1; Comp2 =20.3 }
      { Name = "Tomas"; Age = 28; Comp1=1.1; Comp2 =29.3 }
      { Name = "Eve"; Age = 2; Comp1=2.1; Comp2 =40.3 }
      { Name = "Suzanne"; Age = 15; Comp1=12.4; Comp2 =26.3} ]

let peopleList = Frame.ofRecords peopleRecds   

let toListOfTuple (frame:Frame<_,_>) tupleType = 
    frame.Rows
    |> Series.mapValues(Series.values >> Seq.toArray)
    |> Series.mapValues(fun x -> FSharpValue.MakeTuple(x, tupleType))
    |> Series.values
    |> Seq.toList

let tupleType = typeof<(string*int*float*float)>

(peopleList, tupleType)
||> toListOfTuple 
|> List.iter(printfn "%A")

Print

("Joe", 51, 12.1, 20.3)
("Tomas", 28, 1.1, 29.3)
("Eve", 2, 2.1, 40.3)
("Suzanne", 15, 12.4, 26.3)
+2
source

All Articles