Deedle Frame.Rows |> Series.windowWhile |>从Windows计算出新列的新框架?

时间:2016-06-25 23:22:28

标签: f# deedle

我正在尝试使用Series.windowWhile函数来窗口化Deedle框架的行。我想根据窗口计算一些统计信息,然后将统计信息添加回行。

以下代码在我到达last.Merge(length)行之后无效,但它有望说明我的目标:

type Person = 
  { Name:string; Age:int; Countries:string list; }

let peopleRecds = 
  [ { Name = "Joe"; Age = 51; Countries = [ "UK"; "US"; "UK"] }
    { Name = "Tomas"; Age = 28; Countries = [ "CZ"; "UK"; "US"; "CZ" ] }
    { Name = "Eve"; Age = 2; Countries = [ "FR" ] }
    { Name = "Suzanne"; Age = 15; Countries = [ "US" ] } ]
// Turn the list of records into data frame 
let peopleList = Frame.ofRecords peopleRecds
// Use the 'Name' column as a key (of type string)
let people = peopleList |> Frame.indexRowsInt "Age" |> Frame.sortRowsByKey
let newPeople =
    people.Rows
    |> Series.windowWhile (fun k1 k2 -> abs (k1 - k2) < 14)
    |> Series.mapValues (fun v ->
        let length = series ["length" => (v |> Series.values |> Seq.length)]
        let last = v |> Series.takeLast 1
        let newLast = last.Merge(length)
        newLast)
    |> Frame.ofRows

我希望newPeople成为people框架,其中包含一个名为length的新列。

1 个答案:

答案 0 :(得分:0)

问题在于vObjectSeries<string>,如果您只需要添加mapValues的计算长度,就可以这样做:

let newPeople' =
    people.Rows
    |> Series.windowWhile (fun k1 k2 -> abs (k1 - k2) < 14)
    |> Series.mapValues (fun v ->
        series ["length" => (v |> Series.values |> Seq.length)]    
    )
    |> Frame.ofRows

people.Merge(newPeople')