我正在尝试使用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
的新列。
答案 0 :(得分:0)
问题在于v
是ObjectSeries<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')