说我有以下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
我想要做的是将Comp1
和Comp2
列合并到peopleList
的新列中,n
的{{1}}尚未确定目前,所以我不能只知道总和两列,可能有Comp(n)
,Comp3
,因此必须以正则表达为基础,类似于密钥导致Comp4
。
我应该做的就是每行Comp
mapRowValues
但是我不知道如何在这里的行级操作。
答案 0 :(得分:2)
如果计划增加' Comp' ,把它们放在一个数组中可能会更好:
type nPerson = { Name:string; Age:int; Comp:float[] }
let npeopleRecds =
[ { Name = "Joe"; Age = 51; Comp = [| 12.1; 20.3 |] }
{ Name = "Tomas"; Age = 28; Comp = [| 1.1; 29.3 |] }
{ Name = "Eve"; Age = 2; Comp = [| 2.1; 40.3 |] }
{ Name = "Suzanne"; Age = 15; Comp = [|12.4; 26.3 |] } ]
然后,您可以轻松添加总和为
的列let npeopleList = Frame.ofRecords npeopleRecds
npeopleList.Format() |> printfn "%s"
let sumseries = npeopleList.GetColumn<float []>("Comp") |> Series.mapValues(fun x -> x |> Array.sum)
npeopleList?TotalComp <- sumseries
npeopleList.Format() |> printfn "%s"
打印:
Name Age Comp
0 -> Joe 51 System.Double[]
1 -> Tomas 28 System.Double[]
2 -> Eve 2 System.Double[]
3 -> Suzanne 15 System.Double[]
Name Age Comp TotalComp
0 -> Joe 51 System.Double[] 32,4
1 -> Tomas 28 System.Double[] 30,4
2 -> Eve 2 System.Double[] 42,4
3 -> Suzanne 15 System.Double[] 38,7
修改强>
如果更改人员记录中的字段是不可接受的 - 您可以使用过滤器:
let allSum =
peopleList.Columns
|> Series.filter(fun k _ -> k.StartsWith("Comp"))
|> Frame.ofColumns
|> Frame.rows
|> Series.mapValues(Series.foldValues(fun acc v -> acc + (v :?> float)) 0.0)
peopleList?TotalComp <- allSum
peopleList.Format() |> printfn "%s"
打印:
Name Age Comp1 Comp2 TotalComp
0 -> Joe 51 12,1 20,3 32,4
1 -> Tomas 28 1,1 29,3 30,4
2 -> Eve 2 2,1 40,3 42,4
3 -> Suzanne 15 12,4 26,3 38,7