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

时间:2016-03-09 14:56:39

标签: f# deedle

说我有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转换为list的{​​{1}}?

1 个答案:

答案 0 :(得分:3)

要创建元组,可以使用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)