F# deedle 如何将框架转换为元组列表

F# deedle how to convert frame to list of tuples

假设我有以下 Frame 个,

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

如何将 peopleList 转换为 tupleslist

创建元组可以使用FSharpValue.MakeTuple

然后你需要指定一个类型或元组:

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")

打印:

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